:root {
  color-scheme: light;
  --ink: #1f2926;
  --muted: #707975;
  --paper: #f4f2ec;
  --card: #fffefa;
  --surface: #ecefe9;
  --surface-strong: #e1e8e3;
  --line: #d9ddd7;
  --brand: #246b5a;
  --brand-dark: #185043;
  --brand-soft: #dcebe5;
  --accent: #e59668;
  --positive: #23705a;
  --negative: #b6534b;
  --shadow: 0 20px 60px rgb(31 41 38 / 14%);
  font-family: "Avenir Next", "Helvetica Neue", ui-sans-serif, system-ui, sans-serif;
  font-synthesis: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  color: var(--ink);
  background: var(--paper);
}

button, input, textarea { font: inherit; }
button { cursor: pointer; }

.shell {
  width: min(100%, 760px);
  min-height: 100vh;
  margin: 0 auto;
  padding: 0 22px calc(96px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: max(20px, env(safe-area-inset-top)) 0 18px;
}
.top-actions { display: flex; align-items: center; gap: 4px; }

.brand { display: inline-flex; align-items: center; gap: 9px; color: var(--ink); font-weight: 700; font-size: 1.4rem; line-height: 1; letter-spacing: -.07em; }
.brand-symbol { width: 28px; height: 28px; overflow: visible; }
.brand-symbol circle:first-child { fill: var(--brand); }
.brand-symbol path { fill: none; stroke: var(--card); stroke-width: 4; stroke-linecap: round; }
.brand-symbol .brand-spark { fill: var(--accent); }
.brand-large { gap: 14px; font-size: clamp(2.5rem, 9vw, 4.2rem); }
.brand-large .brand-symbol { width: 52px; height: 52px; }
.auth-brand { margin-bottom: 34px; }
.brand-mark { color: var(--brand); }

.eyebrow { margin: 0 0 8px; color: var(--brand); font-size: .72rem; font-weight: 700; letter-spacing: .11em; }
h1, h2, h3, p { text-wrap: pretty; }
h1, h2, h3 { font-family: "Avenir Next", "Helvetica Neue", sans-serif; }
h1 { margin: 0; font-size: clamp(2.15rem, 10vw, 4rem); font-weight: 700; line-height: .98; letter-spacing: -.055em; }
h2 { margin: 0; font-size: 1.32rem; font-weight: 700; line-height: 1.15; letter-spacing: -.035em; }
h3 { margin: 0; font-size: 1rem; font-weight: 700; letter-spacing: -.02em; }
.muted { color: var(--muted); }

.button {
  min-height: 46px;
  border: 0;
  border-radius: 999px;
  padding: 0 19px;
  color: white;
  background: var(--brand);
  font-weight: 700;
  transition: transform .12s ease, background .12s ease, opacity .12s ease;
}
.button:hover { background: var(--brand-dark); }
.button:active { transform: scale(.97); }
.button:disabled { cursor: not-allowed; opacity: .45; }
.button.secondary { color: var(--brand-dark); background: var(--brand-soft); }
.button.secondary:hover { background: #cce0d8; }
.button.ghost { min-height: 38px; padding: 0 8px; color: var(--muted); background: transparent; }
.button.danger { color: white; background: var(--negative); }
.button.danger:hover { background: #96423c; }
.button.full { width: 100%; }
.icon-button { display: grid; place-items: center; width: 44px; height: 44px; padding: 0; border: 0; border-radius: 50%; color: var(--ink); background: var(--surface); font-size: 1.2rem; font-weight: 700; }
.icon-button:hover { background: var(--surface-strong); }

.hero { padding: 7vh 0 34px; }
.hero p { max-width: 34rem; margin: 18px 0 0; color: var(--muted); font-size: 1.05rem; line-height: 1.6; }
.auth-card, .card { border: 0; border-radius: 24px; background: var(--card); box-shadow: 0 12px 42px rgb(31 41 38 / 8%); }
.auth-card { padding: 7px; }
.auth-body { padding: 22px 14px 14px; }

.tabs, .segments { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 3px; padding: 4px; border-radius: 999px; background: var(--surface); }
.tabs button, .segments button { display: grid; place-items: center; width: 100%; min-height: 46px; border: 0; border-radius: 999px; color: var(--muted); background: transparent; font-weight: 650; text-align: center; cursor: pointer; user-select: none; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
.tabs button.active, .segments button.active { color: white; background: var(--brand); }
.segments button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.stack { display: grid; gap: 17px; }
.field { display: grid; gap: 7px; }
.field > span, .field legend { font-size: .82rem; font-weight: 700; }
input, textarea {
  width: 100%;
  min-height: 48px;
  border: 1px solid transparent;
  border-radius: 11px;
  padding: 11px 13px;
  color: var(--ink);
  background: var(--surface);
  outline: none;
  transition: background .12s ease, border-color .12s ease, box-shadow .12s ease;
}
textarea { min-height: 80px; resize: vertical; }
input:hover, textarea:hover { background: #e7eae5; }
input:focus, textarea:focus { border-color: var(--brand); background: var(--card); box-shadow: 0 0 0 3px var(--brand-soft); }
.error { min-height: 1.2em; margin: 0; color: var(--negative); font-size: .86rem; }

.section-head { display: flex; justify-content: space-between; align-items: end; gap: 16px; margin: 25px 0 10px; }
.group-list, .expense-list { display: grid; gap: 0; }
.group-card { display: flex; align-items: center; justify-content: space-between; gap: 14px; width: 100%; padding: 17px 2px; border: 0; border-bottom: 1px solid var(--line); color: inherit; background: transparent; text-align: left; transition: color .12s ease, transform .12s ease; }
.group-card:hover { color: var(--brand); transform: translateX(3px); }
.group-icon { display: grid; place-items: center; flex: 0 0 42px; height: 42px; border-radius: 50%; color: var(--brand); background: var(--brand-soft); font-size: 1.1rem; }
.group-copy { flex: 1; min-width: 0; }
.group-copy p { margin: 4px 0 0; color: var(--muted); font-size: .86rem; }
.empty { padding: 42px 24px; border: 0; border-radius: 20px; background: rgb(255 254 250 / 62%); text-align: center; }
.empty .emoji { font-size: 2rem; }

.balance-strip { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(140px, 1fr); gap: 9px; margin: 20px -22px 0; padding: 0 22px 6px; overflow-x: auto; scrollbar-width: none; }
.balance { padding: 15px; border: 0; border-radius: 15px; background: var(--card); }
.balance:nth-child(even) { background: var(--brand-soft); }
.balance p { margin: 0 0 8px; color: var(--muted); font-size: .8rem; }
.balance strong { font-size: 1.05rem; }
.positive { color: var(--positive); }
.negative { color: var(--negative); }

.expense { display: grid; grid-template-columns: 42px 1fr auto; gap: 13px; align-items: center; width: 100%; padding: 14px 2px; border: 0; border-bottom: 1px solid var(--line); color: inherit; background: transparent; font: inherit; text-align: left; touch-action: manipulation; transition: color .12s ease, transform .12s ease; }
.expense:hover { color: var(--brand); transform: translateX(3px); }
.expense:focus-visible { border-radius: 10px; outline: 3px solid var(--brand-soft); outline-offset: 2px; }
.expense-date { display: grid; place-items: center; height: 44px; border: 0; border-radius: 12px; color: var(--brand); background: var(--brand-soft); font-size: .68rem; font-weight: 700; line-height: 1.05; text-align: center; }
.expense-date b { color: var(--ink); font-size: 1.08rem; }
.expense p { margin: 3px 0 0; color: var(--muted); font-size: .8rem; }
.expense-amount { color: var(--ink); text-align: right; font-weight: 700; white-space: nowrap; }
.edit-mark { margin-left: 3px; color: var(--muted); font-size: 1.2rem; }

.count-badge { display: grid; place-items: center; min-width: 29px; height: 29px; padding: 0 8px; border-radius: 999px; color: var(--brand); background: var(--brand-soft); font-size: .82rem; font-weight: 700; }
.fab { position: fixed; right: max(20px, calc((100vw - 720px) / 2)); bottom: calc(20px + env(safe-area-inset-bottom)); min-height: 56px; padding: 0 23px; border: 0; border-radius: 999px; color: white; background: var(--brand); box-shadow: 0 10px 28px rgb(36 107 90 / 24%); font-weight: 700; transition: transform .12s ease, background .12s ease; }
.fab:hover { background: var(--brand-dark); }
.fab:active { transform: scale(.96); }

dialog { width: min(calc(100% - 24px), 440px); max-height: calc(100dvh - 24px); margin: auto; border: 0; border-radius: 24px; padding: 0; color: var(--ink); background: var(--card); box-shadow: var(--shadow); }
dialog::backdrop { background: rgb(22 31 28 / 45%); backdrop-filter: blur(4px); }
.confirm-dialog { width: min(calc(100% - 32px), 340px); }
.confirm-dialog p { margin: 8px 0 0; line-height: 1.45; }
.confirm-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.expense-actions { display: flex; gap: 10px; }
.expense-actions #save-expense { flex: 1; }
.dialog-body { padding: 22px; overflow: auto; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 22px; }
.amount-input { position: relative; }
.amount-field, .date-field { width: 50%; }
.amount-input input { height: 58px; padding-right: 40px; font-size: 1.45rem; font-weight: 700; letter-spacing: -.025em; }
.amount-input span { position: absolute; right: 14px; top: 18px; color: var(--muted); font-weight: 700; }

.member-split { display: grid; grid-template-columns: auto minmax(0, 1fr) 122px 44px; align-items: center; gap: 8px; min-height: 64px; border-bottom: 1px solid var(--line); transition: opacity .15s ease; }
.member-split.excluded { opacity: .42; }
.member-split input[type="checkbox"], .member-split input[type="radio"] { width: 21px; min-height: 21px; accent-color: var(--brand); }
.share-cell { width: 122px; min-width: 0; text-align: right; }
.equal-share { font-size: .86rem; font-weight: 700; white-space: nowrap; }
.share-editor { display: flex; align-items: center; justify-content: end; gap: 4px; }
.member-split .share-input { width: 102px; min-height: 38px; padding: 6px 7px; border-radius: 9px; font-size: .88rem; text-align: right; font-weight: 700; }
.share-suffix { position: static; color: var(--muted); font-size: .8rem; font-weight: 700; pointer-events: none; }
.member-name { overflow: hidden; font-weight: 650; text-overflow: ellipsis; white-space: nowrap; }
.payer-label { color: var(--muted); font-size: .68rem; font-weight: 650; }
.payer-control { display: grid; justify-items: center; gap: 2px; cursor: pointer; }
.split-summary { display: flex; justify-content: space-between; gap: 12px; padding: 12px 0 4px; color: var(--muted); font-size: .84rem; }
.split-summary strong { color: var(--ink); }
.split-summary.invalid, .split-summary.invalid strong { color: var(--negative); }

.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 10; transform: translate(-50%, 18px); max-width: calc(100% - 40px); padding: 12px 17px; border-radius: 999px; color: white; background: var(--ink); box-shadow: 0 8px 28px rgb(31 41 38 / 20%); font-size: .9rem; opacity: 0; pointer-events: none; transition: .2s ease; }
.toast.show { transform: translate(-50%, 0); opacity: 1; }

@media (min-width: 700px) {
  .shell { padding-inline: 30px; }
  .auth-layout { display: grid; grid-template-columns: 1.08fr .92fr; align-items: center; gap: 58px; min-height: 100vh; }
  .hero { padding: 0; }
  .auth-card { padding: 9px; }
  .auth-body { padding: 25px 17px 17px; }
  .balance-strip { margin-inline: 0; padding-inline: 0; }
}

@media (max-width: 640px) {
  #expense-dialog {
    inset: 0;
    width: 100vw;
    max-width: none;
    height: 100dvh;
    max-height: none;
    margin: 0;
    overflow: hidden;
    border-radius: 0;
  }
  #expense-dialog .dialog-body {
    height: 100%;
    padding: max(18px, env(safe-area-inset-top)) 18px max(20px, env(safe-area-inset-bottom));
    overflow-y: auto;
    overscroll-behavior: contain;
  }
}

@media (max-width: 520px) {
  dialog { width: 100%; max-height: 92dvh; margin: auto 0 0; border-radius: 24px 24px 0 0; }
  #expense-dialog { max-height: none; margin: 0; border-radius: 0; }
  .confirm-dialog { width: calc(100% - 32px); margin: auto; border-radius: 22px; }
  .member-split { grid-template-columns: auto minmax(62px, 1fr) 122px 40px; gap: 6px; }
  .payer-label { font-size: .62rem; }
}

@media (prefers-reduced-motion: reduce) { *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; } }
