:root {
  --brand: #467987;
  --brand-dark: #386673;
  --canvas: #f0f4f5;
  --surface: #ffffff;
  --surface-soft: #f8fafb;
  --ink: #23343b;
  --muted: #61747e;
  --line: #dce6e9;
  --income: #2d7a63;
  --expense: #b15c67;
  --paid: #e9f4ed;
  --late: #faeaec;
  --focus: #397c8d;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

* { box-sizing: border-box; }
body { margin: 0; min-width: 320px; background: var(--canvas); }
button, input, select { font: inherit; -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid color-mix(in srgb, var(--focus) 42%, transparent); outline-offset: 2px; }

.app-shell { width: min(100%, 42rem); min-height: 100dvh; margin: 0 auto; padding: 0 0 6.8rem; background: var(--surface-soft); }
.topbar { display: grid; grid-template-columns: 4.5rem 1fr 4.5rem; align-items: center; min-height: calc(4.35rem + env(safe-area-inset-top)); padding: calc(.75rem + env(safe-area-inset-top)) 1rem .45rem; background: var(--brand); color: white; }
.topbar > span { display: block; }
.topbar strong { justify-self: center; font-size: .94rem; font-weight: 500; letter-spacing: 0; }
.header-action, .edit-detail { justify-self: end; min-height: 2.4rem; padding: .35rem .5rem; border: 0; border-radius: .45rem; background: transparent; color: inherit; font-size: .85rem; font-weight: 500; }
.header-action:hover, .edit-detail:hover { background: rgba(255,255,255,.12); }
.header-action:active, .edit-detail:active, .add-button:active, .primary-button:active { transform: scale(.98); }

.month-bar { display: grid; grid-template-columns: 3rem 1fr 3rem; align-items: center; min-height: 3.15rem; padding: 0 .4rem; background: var(--surface); border-bottom: 1px solid var(--line); }
.month-name, .month-arrow { border: 0; background: transparent; color: var(--brand-dark); }
.month-name { min-height: 2.8rem; font-size: .94rem; font-weight: 500; text-transform: capitalize; }
.month-arrow { min-height: 2.5rem; font-size: 2rem; line-height: 1; }
.month-arrow:hover, .month-name:hover { background: var(--surface-soft); }

.transactions-section { padding-bottom: 1.25rem; }
.transactions-list, .recurring-list { margin: 0; padding: 0; list-style: none; }
.transaction { border-bottom: 1px solid var(--line); }
.transaction-open { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 1rem; width: 100%; padding: .84rem 1rem; border: 0; background: var(--surface); color: var(--ink); text-align: left; transition: background .18s ease, transform .18s ease; }
.transaction-open:hover { background: #f9fbfc; }
.transaction-open:active { transform: scale(.995); }
.transaction.settled .transaction-open { background: var(--paid); }
.transaction.overdue .transaction-open { background: var(--late); }
.transaction-main { display: grid; min-width: 0; gap: .18rem; }
.transaction-main strong { overflow: hidden; font-size: .96rem; font-weight: 500; line-height: 1.3; text-overflow: ellipsis; white-space: nowrap; }
.transaction-main span { color: var(--muted); font-size: .8rem; }
.transaction-value { display: grid; align-content: center; justify-items: end; gap: .12rem; }
.transaction-value strong { color: var(--expense); font-size: .92rem; font-weight: 500; white-space: nowrap; }
.transaction.income .transaction-value strong { color: var(--income); }
.transaction-value small { min-height: 1rem; color: var(--muted); font-size: .75rem; }
.empty-state { max-width: 26rem; margin: 0 auto; padding: 3.5rem 1.5rem; color: var(--muted); font-size: .92rem; line-height: 1.55; text-align: center; }

.balance-drawer { position: fixed; z-index: 3; bottom: 0; left: 50%; width: min(100%, 42rem); transform: translateX(-50%); background: var(--brand-dark); color: white; box-shadow: 0 -7px 18px rgba(33, 73, 85, .11); }
.balance-trigger { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; width: 100%; min-height: 4.5rem; padding: .75rem 1rem; border: 0; background: transparent; color: inherit; font-size: .92rem; font-weight: 500; text-align: left; }
.balance-trigger strong { text-align: right; }
.balance-chevron { padding: 0 .9rem; font-size: .9rem; transition: transform .2s ease; }
.balance-trigger[aria-expanded="true"] .balance-chevron { transform: rotate(180deg); }
.balance-details { padding: .45rem 1rem .8rem; border-top: 1px solid rgba(255,255,255,.22); }
.balance-details p { display: flex; justify-content: space-between; min-height: 1.7rem; margin: 0; color: white; font-size: .82rem; line-height: 1.7rem; }
.balance-details p:last-child { margin-top: .35rem; padding-top: .35rem; border-top: 1px solid rgba(255,255,255,.22); color: white; font-size: .86rem; font-weight: 500; }
.add-button { position: fixed; z-index: 4; right: max(1rem, calc((100% - 42rem) / 2 + 1rem)); bottom: calc(max(1rem, env(safe-area-inset-bottom)) + 5.55rem); display: grid; width: 3.2rem; min-height: 3.2rem; place-items: center; padding: 0; border: 0; border-radius: 50%; background: var(--brand); color: white; box-shadow: 0 6px 16px rgba(33, 73, 85, .18); font-size: 1.72rem; font-weight: 400; transition: transform .18s ease, background .18s ease; }
body.balance-open .add-button { bottom: calc(max(1rem, env(safe-area-inset-bottom)) + 15.6rem); }
.add-button:hover { background: var(--brand-dark); transform: translateY(-2px); }

.sheet { width: min(100%, 42rem); min-height: 100dvh; max-height: none; margin: 0 auto; padding: .25rem; border: 0; background: var(--surface-soft); color: var(--ink); }
.sheet::backdrop { background: rgba(26, 48, 57, .38); }
.sheet > div, .sheet > form { min-height: 100dvh; padding: 0 1rem max(1.75rem, env(safe-area-inset-bottom)); }
.sheet-app-header { display: grid; grid-template-columns: 4.5rem 1fr 4.5rem; align-items: center; min-height: calc(4.35rem + env(safe-area-inset-top)); margin: -.25rem -1rem 1.35rem; padding: calc(.75rem + env(safe-area-inset-top)) 1rem .45rem; background: var(--brand); color: white; text-align: center; }
.sheet-app-header strong { font-size: .92rem; font-weight: 500; }
.back-button { display: grid; width: 2.4rem; min-height: 2.4rem; place-items: center; padding: 0; border: 0; border-radius: .5rem; background: transparent; color: white; font-size: 2rem; line-height: 1; }
.back-button:hover { background: rgba(255,255,255,.12); }
.type-selector { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; margin: 0 0 1rem; padding: 0; border: 0; }
.type-selector legend, .visually-hidden { position: absolute; overflow: hidden; width: 1px; height: 1px; clip: rect(0 0 0 0); white-space: nowrap; }
.type-selector label { display: grid; min-height: 2.75rem; place-items: center; border: 1px solid var(--line); border-radius: .5rem; background: var(--surface); color: var(--muted); font-size: .82rem; font-weight: 500; }
.type-selector input { position: absolute; opacity: 0; }
.type-selector label:has(input:checked) { border-color: var(--brand); background: var(--brand); color: white; }
.field { display: grid; gap: .35rem; margin: .7rem 0; }
.field > span, .field small { color: var(--muted); font-size: .75rem; line-height: 1.4; }
.field input, .field select { width: 100%; min-height: 3rem; padding: .72rem .78rem; border: 1px solid var(--line); border-radius: .5rem; background: var(--surface); color: var(--ink); font-size: .9rem; outline: none; }
.field input:focus, .field select:focus { border-color: var(--focus); box-shadow: 0 0 0 3px rgba(10,109,137,.14); }
.field input[type="date"] { text-align: center; }
#repeat-control { margin: 1rem 0; }
.repeat-control { display: flex; align-items: center; justify-content: space-between; gap: 1rem; width: 100%; color: var(--ink); font-size: .88rem; font-weight: 500; }
.repeat-control[hidden] { display: none; }
.repeat-control input { position: absolute; opacity: 0; }
.repeat-control i { position: relative; display: block; width: 2.75rem; height: 1.55rem; border-radius: 2rem; background: #bac6cc; transition: background .2s ease; }
.repeat-control i::after { position: absolute; top: .18rem; left: .18rem; width: 1.19rem; height: 1.19rem; border-radius: 50%; background: white; box-shadow: 0 1px 3px rgba(0,0,0,.18); content: ""; transition: transform .2s ease; }
.repeat-control input:checked + i { background: var(--brand); }
.repeat-control input:checked + i::after { transform: translateX(1.2rem); }
.repeat-options { display: grid; gap: .62rem; margin: .9rem 0; padding: .85rem; border: 1px solid var(--line); border-radius: .5rem; background: var(--surface); }
.repeat-options[hidden], .installment-field[hidden] { display: none; }
.repeat-options label { display: flex; align-items: center; gap: .65rem; color: var(--ink); font-size: .86rem; }
.repeat-options input { width: 1.1rem; height: 1.1rem; margin: 0; accent-color: var(--brand); }
.installment-field { padding: .85rem; border: 1px solid var(--line); border-radius: .5rem; background: var(--surface); }
.primary-button, .secondary-button { display: grid; width: 100%; min-height: 3rem; place-items: center; border-radius: .5rem; font-size: .88rem; font-weight: 500; text-decoration: none; transition: transform .18s ease, background .18s ease; }
.primary-button { margin-top: 1rem; border: 0; background: var(--brand); color: white; }
.primary-button:hover { background: var(--brand-dark); }
.secondary-button { margin-top: .7rem; border: 1px solid var(--line); background: var(--surface); color: var(--ink); }
.secondary-button:hover { background: #f2f6f7; }
.import-label { cursor: pointer; }
.import-label input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.import-options { display: grid; gap: .2rem; margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--line); }
.import-options .primary-button, .import-options .secondary-button { margin-top: .5rem; }
.text-button { width: fit-content; min-height: 2.75rem; margin: .2rem auto 0; padding: .4rem .75rem; border: 0; background: transparent; color: var(--muted); font-size: .84rem; font-weight: 500; }
.text-button:hover { color: var(--ink); }

.details-body { max-width: 28rem; margin: 0 auto; text-align: center; }
.type-banner { margin: 0 0 1.5rem; padding: .72rem; border-radius: .4rem; background: var(--expense); color: white; font-size: .82rem; font-weight: 500; }
.type-banner.income { background: var(--income); }
.detail-description { display: block; margin-bottom: 1rem; font-size: 1rem; font-weight: 500; }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; margin-bottom: 1rem; border: 1px solid var(--line); border-radius: .5rem; overflow: hidden; background: var(--surface); }
.detail-grid span, .detail-grid button { min-height: 3rem; padding: .7rem; border: 0; color: var(--ink); font-size: .88rem; }
.detail-grid span + span { border-top: 1px solid var(--line); }
.detail-grid button { grid-row: span 2; border-left: 1px solid var(--line); background: var(--surface-soft); color: var(--brand-dark); font-weight: 500; }
.detail-grid button:hover { background: #e7f0f2; }
.details-body .primary-button { width: 100%; margin-top: 0; background: var(--expense); }
.details-body .primary-button:hover { background: #8f303b; }
.sheet-header { display: flex; align-items: center; justify-content: space-between; padding-top: 1rem; }
.sheet-header h2 { margin: .2rem 0; font-size: 1.25rem; }
.eyebrow { margin: 0; color: var(--muted); font-size: .72rem; font-weight: 500; letter-spacing: .04em; text-transform: uppercase; }
.sheet-copy { color: var(--muted); font-size: .9rem; line-height: 1.6; }
.close-button { display: grid; width: 2.4rem; min-height: 2.4rem; place-items: center; border: 0; border-radius: .5rem; background: transparent; color: var(--muted); font-size: 1.7rem; }
.close-button:hover { background: var(--canvas); }
.recurring-list { margin-top: 1rem; }
.recurring-list li { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .85rem 0; border-bottom: 1px solid var(--line); }
.recurring-list div { display: grid; gap: .25rem; }
.recurring-list span { color: var(--muted); font-size: .76rem; }
.recurring-list button { border: 0; background: transparent; color: var(--expense); font-size: .82rem; }

@media (min-width: 700px) {
  .app-shell { min-height: 46rem; margin-top: 2rem; margin-bottom: 2rem; border: 1px solid var(--line); border-radius: .8rem; overflow: hidden; box-shadow: 0 18px 46px rgba(18, 54, 67, .12); }
  .balance-drawer { bottom: 2rem; border-radius: 0 0 .8rem .8rem; overflow: hidden; }
  .add-button { bottom: calc(2rem + 5.55rem); }
  body.balance-open .add-button { bottom: calc(2rem + 15.6rem); }
  .sheet { min-height: 42rem; margin: auto; border: 1px solid var(--line); border-radius: .8rem; overflow: hidden; box-shadow: 0 18px 46px rgba(18, 54, 67, .24); }
  .sheet > div, .sheet > form { min-height: 42rem; }
}
