:root {
    --bg: #f4f7f6;
    --surface: #ffffff;
    --surface-soft: #edf7f3;
    --text: #17211e;
    --muted: #697773;
    --line: #dce6e2;
    --primary: #0d7c66;
    --primary-dark: #075c4c;
    --primary-light: #d9f1e8;
    --positive: #087f5b;
    --negative: #c74747;
    --danger-bg: #fff0f0;
    --shadow: 0 8px 28px rgba(18, 52, 43, .08);
    --radius: 18px;
    --radius-small: 12px;
    --header-height: 70px;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--text);
    background: var(--bg);
    font-synthesis: none;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body { margin: 0; min-height: 100vh; background: radial-gradient(circle at 50% -20%, #e3f4ed 0, transparent 42rem), var(--bg); }
body.is-authenticated { padding-bottom: 82px; }
a { color: var(--primary); text-decoration: none; }
button, input, select, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: .45rem; font-size: clamp(1.8rem, 6vw, 2.7rem); line-height: 1.08; letter-spacing: -.035em; }
h2 { margin-bottom: .35rem; font-size: 1.25rem; letter-spacing: -.02em; }
h3 { margin-bottom: .25rem; }
p { color: var(--muted); line-height: 1.55; }
small { color: var(--muted); }

.app-header {
    position: sticky; top: 0; z-index: 20;
    height: var(--header-height); padding: 0 max(18px, env(safe-area-inset-left));
    display: flex; align-items: center; justify-content: space-between;
    background: rgba(255, 255, 255, .88); backdrop-filter: blur(18px);
    border-bottom: 1px solid rgba(220, 230, 226, .8);
}
.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--text); font-weight: 800; font-size: 1.08rem; }
.brand-mark { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 12px; color: #fff; background: linear-gradient(145deg, #14a683, #096754); box-shadow: 0 8px 20px rgba(13,124,102,.25); }
.brand-mark-large { width: 58px; height: 58px; border-radius: 18px; font-size: 1.5rem; margin-bottom: 1.4rem; }
.header-user { display: flex; align-items: center; gap: 8px; }
.header-user form { margin: 0; }
.header-name { display: none; font-weight: 650; font-size: .9rem; }

.app-shell { width: min(1120px, 100%); margin: 0 auto; padding: 24px 16px 40px; }
.card { background: var(--surface); border: 1px solid rgba(220,230,226,.9); border-radius: var(--radius); box-shadow: var(--shadow); }
.eyebrow { display: block; margin-bottom: 6px; color: var(--primary); font-size: .72rem; font-weight: 800; letter-spacing: .11em; text-transform: uppercase; }
.page-heading { margin: 10px 0 24px; }
.page-heading p { max-width: 620px; margin-bottom: 0; }
.page-heading-with-action { display: flex; justify-content: space-between; align-items: flex-end; gap: 20px; }
.back-link { display: inline-block; margin-bottom: 22px; color: var(--muted); font-size: .9rem; font-weight: 650; }
.back-link:hover { color: var(--primary); }
.desktop-action { display: none; }

.button {
    min-height: 44px; padding: 10px 16px; border: 1px solid transparent; border-radius: 12px;
    display: inline-flex; align-items: center; justify-content: center; gap: 7px;
    font-weight: 750; cursor: pointer; transition: transform .15s ease, background .15s ease, border-color .15s ease;
}
.button:active { transform: translateY(1px); }
.button-primary { color: #fff; background: var(--primary); box-shadow: 0 7px 16px rgba(13,124,102,.18); }
.button-primary:hover { background: var(--primary-dark); }
.button-secondary { color: var(--primary); background: var(--surface); border-color: #b7d7ce; }
.button-ghost { color: var(--primary); background: transparent; border-color: transparent; }
.button-danger { color: var(--negative); background: var(--danger-bg); border-color: #f3caca; }
.button-small { min-height: 36px; padding: 7px 10px; font-size: .82rem; }
.button-block { width: 100%; }

.flash, .validation-errors { margin-bottom: 18px; padding: 14px 16px; border-radius: 13px; border: 1px solid; font-size: .93rem; }
.flash-success { color: #086047; background: #e5f7f0; border-color: #b7e5d4; }
.flash-error, .validation-errors { color: #9d3232; background: #fff0f0; border-color: #f1c5c5; }
.validation-errors strong { display: block; margin-bottom: 6px; }
.validation-errors ul { margin: 0; padding-left: 20px; }

.auth-page { min-height: 100vh; padding: 30px 16px; display: grid; align-content: center; gap: 28px; }
.auth-hero { max-width: 540px; margin: 0 auto; }
.auth-hero h1 { max-width: 500px; }
.auth-hero p { font-size: 1.04rem; }
.auth-card { width: min(100%, 460px); margin: 0 auto; padding: 24px; }
.auth-switch { margin: 20px 0 0; text-align: center; font-size: .9rem; }

.form-stack { display: grid; gap: 16px; }
.form-grid { display: grid; gap: 16px; }
label > span { display: block; margin-bottom: 7px; color: #35413d; font-size: .86rem; font-weight: 720; }
label > span small { font-weight: 500; }
input, select, textarea {
    width: 100%; min-height: 46px; padding: 11px 13px; color: var(--text); background: #fbfcfc;
    border: 1px solid #cad8d3; border-radius: 11px; outline: none; transition: border-color .15s, box-shadow .15s, background .15s;
}
textarea { min-height: 86px; resize: vertical; }
input:focus, select:focus, textarea:focus { background: #fff; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(13,124,102,.12); }
label > small { display: block; margin-top: 5px; }
.form-card { padding: 20px; }
.form-card + .form-card { margin-top: 16px; }
.narrow-card { max-width: 680px; }
.amount-input { font-size: 1.15rem; font-weight: 780; }

.balance-hero { padding: 24px; margin-bottom: 22px; color: #fff; background: linear-gradient(135deg, #213c35, #0d7c66); border-radius: 22px; box-shadow: 0 14px 35px rgba(12,78,64,.2); }
.balance-hero span, .balance-hero small { display: block; color: rgba(255,255,255,.78); }
.balance-hero strong { display: block; margin: 4px 0; font-size: clamp(1.9rem, 8vw, 2.65rem); letter-spacing: -.04em; }
.balance-hero.balance-negative { background: linear-gradient(135deg, #603737, #b34a4a); }
.balance-hero.balance-positive { background: linear-gradient(135deg, #164d3d, #07835e); }

.invitation-stack { margin-bottom: 24px; }
.invitation-card { padding: 14px; display: grid; grid-template-columns: 44px minmax(0, 1fr); align-items: center; gap: 12px; }
.invitation-card + .invitation-card { margin-top: 10px; }
.invitation-card strong, .invitation-card small { display: block; }
.invitation-card small { margin-top: 4px; }
.invitation-actions { grid-column: 1 / -1; display: flex; justify-content: flex-end; gap: 7px; }
.invitation-actions form { margin: 0; }
.invite-link-card { margin-bottom: 16px; padding: 18px; border-color: #acdaca; background: linear-gradient(135deg, #fff, #eef9f5); }
.invite-link-card p { margin-bottom: 14px; }
.copy-field { display: flex; gap: 8px; }
.copy-field input { min-width: 0; font-size: .78rem; }
.copy-field .button { flex: 0 0 auto; }
.invitation-page { min-height: calc(100vh - 130px); display: grid; place-items: center; padding: 20px 0; }
.invitation-hero-card { width: min(100%, 650px); padding: clamp(24px, 6vw, 44px); text-align: center; }
.invitation-hero-card .group-icon { margin: 0 auto 20px; }
.invitation-hero-card p { max-width: 500px; margin-left: auto; margin-right: auto; }
.invitation-meta { margin: 24px 0; padding: 14px; display: flex; flex-wrap: wrap; justify-content: center; gap: 10px 24px; background: #f3f7f6; border-radius: 13px; }
.invitation-meta span { color: var(--muted); font-size: .8rem; }
.invitation-meta strong { color: var(--text); }
.invitation-decision { display: flex; flex-wrap: wrap; justify-content: center; gap: 9px; }
.invitation-decision form { margin: 0; }
.invitation-help { margin-top: 22px; }

.group-grid { display: grid; gap: 14px; }
.group-card { display: block; padding: 18px; color: var(--text); transition: transform .18s, box-shadow .18s; }
.group-card:hover { transform: translateY(-2px); box-shadow: 0 14px 34px rgba(18,52,43,.12); }
.group-card-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; }
.group-icon { display: grid; place-items: center; width: 44px; height: 44px; color: var(--primary-dark); background: var(--primary-light); border-radius: 14px; font-weight: 850; }
.group-icon-large { flex: 0 0 auto; width: 58px; height: 58px; border-radius: 18px; font-size: 1.35rem; }
.member-count { color: var(--muted); font-size: .78rem; }
.group-card h2 { font-size: 1.2rem; }
.group-card p { display: -webkit-box; overflow: hidden; -webkit-line-clamp: 2; -webkit-box-orient: vertical; min-height: 2.6em; font-size: .88rem; }
.group-balance { margin-top: 18px; padding-top: 14px; display: flex; align-items: end; justify-content: space-between; border-top: 1px solid var(--line); }
.group-balance span { color: var(--muted); font-size: .8rem; }
.group-balance strong { font-size: 1.05rem; }
.text-positive { color: var(--positive) !important; }
.text-negative { color: var(--negative) !important; }

.empty-state { padding: 36px 24px; text-align: center; }
.empty-state p { max-width: 440px; margin-left: auto; margin-right: auto; }
.empty-icon { width: 58px; height: 58px; margin: 0 auto 18px; display: grid; place-items: center; color: var(--primary); background: var(--primary-light); border-radius: 19px; font-size: 1.5rem; font-weight: 800; }
.compact-empty { padding: 28px 20px; }
.error-state { max-width: 620px; margin: 10vh auto 0; }

.group-header { margin: 8px 0 22px; }
.group-title-row { display: flex; align-items: flex-start; gap: 14px; }
.group-title-row p { margin-bottom: 0; }
.group-actions { margin-top: 22px; display: grid; grid-template-columns: 1fr; gap: 9px; }
.section-heading { margin-bottom: 18px; display: flex; justify-content: space-between; align-items: center; gap: 14px; }
.section-heading h2 { margin: 0; }
.section-heading > div { min-width: 0; }
.balances-card, .settlement-card { margin-bottom: 16px; padding: 20px; }
.member-balances { display: grid; }
.member-balance-row { padding: 12px 0; display: flex; align-items: center; gap: 11px; border-top: 1px solid var(--line); }
.member-balance-row:first-child { border-top: 0; }
.avatar { flex: 0 0 auto; display: grid; place-items: center; width: 40px; height: 40px; color: #1c6656; background: #dff2eb; border-radius: 50%; font-weight: 800; }
.avatar-small { width: 32px; height: 32px; font-size: .78rem; }
.member-details { min-width: 0; flex: 1; }
.member-details strong, .member-details small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-details small { margin-top: 2px; font-size: .75rem; }
.member-amount { text-align: right; }
.member-amount small, .member-amount strong { display: block; }
.member-amount small { font-size: .72rem; }
.member-amount strong { margin-top: 2px; font-size: .9rem; }

.settlement-list { display: grid; gap: 8px; }
.settlement-suggestion { width: 100%; padding: 13px; display: flex; align-items: center; justify-content: space-between; gap: 12px; text-align: left; color: var(--text); background: #f5faf8; border: 1px solid #d7e9e3; border-radius: 12px; cursor: pointer; }
.settlement-suggestion span { font-size: .86rem; }
.settlement-suggestion b { flex: 0 0 auto; color: var(--primary); }
.all-settled-banner { margin-bottom: 16px; padding: 13px 16px; color: var(--positive); background: #e6f7f0; border: 1px solid #bde6d7; border-radius: 13px; font-weight: 720; text-align: center; }

.content-grid { display: grid; gap: 24px; }
.list-heading { margin: 25px 2px 12px; }
.expense-list { display: grid; gap: 10px; }
.expense-row { position: relative; padding: 14px; display: grid; grid-template-columns: 44px minmax(0,1fr) auto; align-items: center; gap: 11px; }
.expense-date { width: 42px; height: 48px; display: grid; place-content: center; text-align: center; color: var(--muted); background: #f2f6f5; border-radius: 10px; line-height: 1; }
.expense-date strong { font-size: 1.05rem; }
.expense-date small { margin-top: 3px; font-size: .61rem; }
.expense-main { min-width: 0; }
.expense-main h3 { margin: 1px 0 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .95rem; }
.expense-main small { font-size: .69rem; }
.expense-category { color: var(--primary); font-size: .66rem; font-weight: 760; }
.expense-value { padding-right: 22px; text-align: right; }
.expense-value strong, .expense-value small { display: block; white-space: nowrap; }
.expense-value strong { font-size: .88rem; }
.expense-value small { margin-top: 4px; font-size: .65rem; }
.icon-button { position: absolute; top: 7px; right: 5px; width: 28px; height: 28px; display: grid; place-items: center; color: var(--muted); font-size: 1.2rem; }
.inline-delete-form { position: absolute; top: 0; right: 0; }
.inline-delete-form .icon-button { position: static; color: var(--negative); background: transparent; border: 0; cursor: pointer; }
.payments-heading { margin-top: 28px; }
.activity-list { padding: 16px; }
.activity-item { display: flex; gap: 10px; }
.activity-dot { flex: 0 0 auto; width: 8px; height: 8px; margin-top: 7px; background: #72b9a6; border-radius: 50%; }
.activity-item p { margin: 0 0 14px; font-size: .8rem; }
.activity-item small { display: block; margin-top: 3px; font-size: .68rem; }
.muted { color: var(--muted); }

.app-dialog { width: min(520px, calc(100% - 24px)); max-height: min(82vh, 720px); padding: 0; color: var(--text); background: var(--surface); border: 0; border-radius: 20px; box-shadow: 0 24px 70px rgba(7,40,32,.27); }
.app-dialog::backdrop { background: rgba(11,27,23,.52); backdrop-filter: blur(3px); }
.dialog-header { position: sticky; top: 0; z-index: 2; padding: 18px 20px; display: flex; justify-content: space-between; align-items: center; background: #fff; border-bottom: 1px solid var(--line); }
.dialog-header h2 { margin: 0; }
.dialog-close { width: 38px; height: 38px; color: var(--muted); background: #f1f5f4; border: 0; border-radius: 50%; font-size: 1.5rem; cursor: pointer; }
.dialog-body { padding: 20px; overflow-y: auto; }
.dialog-body hr { margin: 22px 0; border: 0; border-top: 1px solid var(--line); }
.member-chip-list { display: grid; gap: 8px; }
.member-chip { padding: 10px; display: flex; align-items: center; gap: 10px; background: #f7faf9; border-radius: 11px; }
.member-chip > span:nth-child(2) { min-width: 0; flex: 1; }
.member-chip strong, .member-chip small { display: block; overflow: hidden; text-overflow: ellipsis; }
.member-chip small { margin-top: 2px; font-size: .7rem; }
.member-chip b { padding: 4px 7px; color: var(--primary); background: var(--primary-light); border-radius: 7px; font-size: .66rem; }
.renew-invite-form { margin: 0; }
.renew-invite-form .button { min-height: 30px; padding: 4px 6px; font-size: .66rem; }

.expense-form { display: grid; gap: 16px; }
.expense-form .form-card + .form-card { margin-top: 0; }
.step-number { display: grid; place-items: center; width: 30px; height: 30px; color: #fff; background: var(--primary); border-radius: 10px; font-size: .82rem; font-weight: 800; }
.section-heading > div:has(.step-number) { display: flex; align-items: center; gap: 10px; }
.section-heading > div:has(.step-number) h2 { margin: 0; }
.split-member-list { display: grid; }
.split-member-row { min-height: 58px; padding: 9px 0; display: flex; align-items: center; gap: 10px; border-top: 1px solid var(--line); }
.split-member-row:first-child { border-top: 0; }
.split-member-name { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .9rem; font-weight: 650; }
.money-field { position: relative; flex: 0 0 122px; }
.money-field input { min-height: 42px; padding-right: 38px; text-align: right; }
.money-field b { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: .7rem; pointer-events: none; }
.check-row > input { flex: 0 0 auto; width: 21px; min-height: 21px; accent-color: var(--primary); }
.check-row > strong { min-width: 72px; text-align: right; font-size: .82rem; }
.form-total-check { margin: 10px 0 0; padding-top: 11px; display: flex; justify-content: space-between; border-top: 1px solid var(--line); font-size: .84rem; }
.form-total-check strong { color: var(--primary); }
.use-full-total { margin-top: 8px; padding-left: 0; }
.segmented-control { margin-bottom: 18px; display: grid; grid-template-columns: repeat(4, 1fr); padding: 4px; background: #edf2f0; border-radius: 12px; }
.segmented-control label { cursor: pointer; }
.segmented-control input { position: absolute; opacity: 0; pointer-events: none; }
.segmented-control span { margin: 0; padding: 9px 5px; display: block; color: var(--muted); border-radius: 9px; text-align: center; font-size: .75rem; }
.segmented-control input:checked + span { color: var(--primary-dark); background: #fff; box-shadow: 0 2px 7px rgba(24,50,43,.1); font-weight: 780; }
.panel-help { margin-bottom: 8px; font-size: .8rem; }
.sticky-form-actions { position: sticky; bottom: 74px; z-index: 10; margin-top: 4px; padding: 10px; display: flex; justify-content: flex-end; gap: 9px; background: rgba(255,255,255,.92); border: 1px solid var(--line); border-radius: 15px; box-shadow: 0 10px 30px rgba(18,52,43,.13); backdrop-filter: blur(12px); }

.bottom-nav { position: fixed; z-index: 30; left: 50%; bottom: max(10px, env(safe-area-inset-bottom)); transform: translateX(-50%); width: min(310px, calc(100% - 28px)); height: 62px; padding: 5px 18px; display: flex; align-items: center; justify-content: space-around; background: rgba(22,39,34,.95); border: 1px solid rgba(255,255,255,.1); border-radius: 21px; box-shadow: 0 12px 35px rgba(8,35,28,.25); backdrop-filter: blur(16px); }
.bottom-nav a { min-width: 70px; display: grid; place-items: center; color: rgba(255,255,255,.75); }
.bottom-nav a > span { font-size: 1.25rem; line-height: 1; }
.bottom-nav small { margin-top: 3px; color: inherit; font-size: .65rem; }
.bottom-nav .bottom-nav-action { color: #fff; }
.bottom-nav-action > span { width: 34px; height: 30px; display: grid; place-items: center; background: var(--primary); border-radius: 10px; }

@media (min-width: 680px) {
    .app-shell { padding: 32px 24px 60px; }
    .header-name { display: inline; }
    .desktop-action { display: inline-flex; }
    .group-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .group-actions { display: flex; }
    .two-columns { grid-template-columns: minmax(0, 1.4fr) minmax(180px, .6fr); }
    .auth-page { grid-template-columns: minmax(300px, 540px) minmax(380px, 460px); align-items: center; justify-content: center; gap: clamp(40px, 8vw, 110px); }
    .auth-card { padding: 32px; }
    .sticky-form-actions { bottom: 18px; }
    .invitation-card { grid-template-columns: 44px minmax(0, 1fr) auto; }
    .invitation-actions { grid-column: auto; }
}

@media (min-width: 920px) {
    body.is-authenticated { padding-bottom: 0; }
    .bottom-nav { display: none; }
    .group-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .content-grid { grid-template-columns: minmax(0, 1fr) 310px; align-items: start; }
    .activity-panel { position: sticky; top: calc(var(--header-height) + 18px); }
    .balances-card { display: inline-block; width: calc(58% - 8px); vertical-align: top; }
    .settlement-card { display: inline-block; width: calc(42% - 8px); margin-left: 12px; vertical-align: top; }
    .expense-form { grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; }
    .expense-form > .form-card:first-of-type { grid-column: 1 / -1; }
    .expense-form > .sticky-form-actions { grid-column: 1 / -1; }
}

@media (max-width: 420px) {
    .app-shell { padding-left: 12px; padding-right: 12px; }
    .app-header { padding-left: 14px; padding-right: 10px; }
    .expense-row { grid-template-columns: 39px minmax(0,1fr); }
    .expense-value { grid-column: 2; padding-right: 0; display: flex; justify-content: space-between; align-items: center; text-align: left; }
    .expense-value small { margin: 0 22px 0 8px; }
    .group-actions .button { width: 100%; }
    .section-heading > small { max-width: 120px; text-align: right; }
}
