:root {
  --bg: #ffffff; --surface: #f3f6f4; --ink: #222a2a; --muted: #64706c; --line: #dce2df;
  --accent: #235e4b; --accent-hover: #194936; --accent-ink: #ffffff; --accent-text: #235e4b; --accent-soft: #e4efe9;
  --error: #ab302d; --error-bg: #fff2f0; --focus: #447b68; --veil: #172a2359; --warn: #9a5b00;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Malgun Gothic", "Apple SD Gothic Neo", sans-serif;
  --tabbar: 64px;
  color-scheme: light; font-family: var(--font); color: var(--ink); background: var(--bg);
  font-size: 16px; line-height: 1.5; font-synthesis: none;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121715; --surface: #1b2220; --ink: #e5ebe8; --muted: #9aa7a2; --line: #2d3734;
    --accent: #2f7a60; --accent-hover: #388c6f; --accent-ink: #ffffff; --accent-text: #74c7a5; --accent-soft: #1f3a30;
    --error: #ff8a80; --error-bg: #3a1f1d; --focus: #74c7a5; --veil: #000000a0; --warn: #f0b860;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; min-width: 0; overflow-x: clip; }
body { background: var(--bg); padding: 0 28px 48px; }
button, input, select, textarea { font: inherit; color: inherit; }
button, a, summary { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
button { cursor: pointer; white-space: nowrap; }
button:disabled { opacity: .55; cursor: wait; }
button, input, select, textarea { border-radius: 6px; }
input, select, textarea { width: 100%; min-width: 0; border: 1px solid var(--line); background: var(--bg); padding: 11px 13px; }
input, select { min-height: 46px; }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: .8; }
textarea { resize: vertical; }
input[type="date"] { appearance: auto; }
a { color: inherit; text-decoration: none; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 26px; font-weight: 750; letter-spacing: -.04em; }
h2 { font-size: 20px; font-weight: 700; letter-spacing: -.03em; }
svg { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.shell { max-width: 1060px; margin: auto; }

/* Header, tabs and menu */
.header { display: flex; align-items: center; gap: 24px; padding: 16px 0; border-bottom: 1px solid var(--line); }
.tabs { display: flex; gap: 4px; margin-left: auto; }
.tabs a { display: flex; align-items: center; gap: 6px; padding: 9px 14px; border-radius: 6px; font-size: 15px; color: var(--muted); font-weight: 500; }
.tabs a:hover { background: var(--surface); }
.tabs a[aria-current="page"] { color: var(--accent-text); background: var(--accent-soft); font-weight: 650; }
.menu { position: relative; }
.menu summary { list-style: none; display: grid; place-items: center; width: 44px; height: 44px; border-radius: 6px; cursor: pointer; color: var(--muted); }
.menu summary::-webkit-details-marker { display: none; }
.menu summary:hover, .menu[open] summary { background: var(--surface); }
.menu-list { position: absolute; right: 0; top: 48px; z-index: 20; min-width: 190px; display: flex; flex-direction: column; padding: 6px; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; }
.menu-list a { padding: 11px 12px; border-radius: 6px; font-size: 14px; }
.menu-list a:hover { background: var(--surface); }
.menu-list button { padding: 11px 12px; border: 0; border-radius: 6px; background: none; font-size: 14px; text-align: left; }
.menu-list button:hover { background: var(--surface); }
.menu-account { padding: 8px 12px 6px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); margin-top: 4px; }

/* Sign-in page */
.login { max-width: 360px; margin: 18vh auto 0; display: flex; flex-direction: column; gap: 16px; }
.login-lead { color: var(--muted); }
.login-message { padding: 10px 12px; border-radius: 6px; background: var(--surface); font-size: 14px; }
.login-button { display: flex; align-items: center; justify-content: center; border-radius: 6px; }
.test-banner { font-size: 13px; background: var(--surface); padding: 8px; text-align: center; margin-top: 12px; border-radius: 6px; }

/* Shared controls */
.primary { color: var(--accent-ink); background: var(--accent); border: 1px solid var(--accent); min-height: 52px; padding: 11px 16px; font-size: 17px; font-weight: 650; }
.primary:hover { background: var(--accent-hover); }
.secondary { background: var(--bg); border: 1px solid var(--line); padding: 9px 16px; min-height: 44px; }
.secondary:hover { background: var(--surface); }
.ghost { display: inline-flex; align-items: center; gap: 7px; background: none; border: 1px solid var(--line); padding: 8px 12px; min-height: 40px; font-size: 14px; color: var(--accent-text); }
.ghost:hover { background: var(--surface); }
.wide { width: 100%; }
.text-button { padding: 8px 4px; border: 0; background: none; font-size: 13px; color: var(--muted); text-decoration: underline; }
.icon-button { display: grid; place-items: center; width: 44px; height: 44px; padding: 0; border: 1px solid var(--line); background: var(--bg); }
.icon-button:hover { background: var(--surface); }
.link { font-size: 14px; color: var(--accent-text); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px; padding: 7px 13px; border: 1px solid var(--line); border-radius: 999px; background: var(--bg); font-size: 15px; line-height: 1.2; cursor: pointer; }
.chip:hover { background: var(--surface); }
.chip[aria-pressed="true"], .chip:has(input:checked) { border-color: var(--accent); background: var(--accent); color: var(--accent-ink); }
.chip.fixed:not(:has(input:checked)) { color: var(--muted); border-style: dashed; }
.chip input[type="radio"] { position: absolute; opacity: 0; width: 1px; height: 1px; pointer-events: none; }
.chip:has(input:focus-visible) { outline: 2px solid var(--focus); outline-offset: 2px; }
.chip svg { width: 18px; height: 18px; }
.bar { display: block; height: 8px; border-radius: 999px; background: var(--surface); overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: inherit; max-width: 100%; }
.bar.over > span { background: var(--error); }
.footnote { margin-top: 14px; font-size: 12px; line-height: 1.65; color: var(--muted); }
.empty { color: var(--muted); padding: 40px 12px; text-align: center; font-size: 15px; }
.error { color: var(--error); font-size: 14px; overflow-wrap: anywhere; }
.muted { color: var(--muted); }
.section-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
[hidden] { display: none !important; }

/* Record view */
.summary { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 24px; padding: 18px 0; border-bottom: 1px solid var(--line); }
.summary-week { font-size: 14px; color: var(--muted); }
.summary-values { display: flex; gap: 24px; font-size: 14px; }
.summary-values strong { font-size: 20px; font-weight: 650; margin-left: 6px; font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--ink); }
.summary-budget { flex-basis: 100%; display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); padding-top: 4px; }
.summary-budget.over { color: var(--error); }
.record-layout { display: grid; grid-template-columns: minmax(0, 420px) minmax(0, 1fr); gap: 56px; padding-top: 24px; align-items: start; }
.composer-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 16px; }
.image-panel { margin-bottom: 18px; padding: 16px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); }
.image-preview-row { display: flex; gap: 12px; align-items: center; min-width: 0; }
.image-preview-row img { width: 68px; height: 90px; object-fit: contain; background: var(--bg); border: 1px solid var(--line); border-radius: 4px; }
.image-preview-row > div { min-width: 0; }
#image-filename { overflow-wrap: anywhere; font-size: 13px; }
#extract-image { margin-top: 12px; font-size: 14px; }
.image-message { margin-top: 10px; font-size: 14px; color: var(--accent-text); overflow-wrap: anywhere; }
.image-message.error { color: var(--error); }
.image-warnings { padding-left: 18px; margin: 8px 0; font-size: 13px; color: var(--muted); }
.image-warnings li + li { margin-top: 5px; }
#image-candidate-label { display: block; margin-top: 12px; font-size: 14px; }
#image-candidate { margin-top: 6px; font-size: 14px; }
#apply-image { margin-top: 10px; font-size: 14px; }
#image-availability { margin: -6px 0 14px; }

.entry-form fieldset { margin: 0; padding: 0; border: 0; min-width: 0; display: flex; flex-direction: column; gap: 18px; }
.field { display: flex; flex-direction: column; gap: 8px; font-size: 14px; font-weight: 550; color: var(--muted); min-width: 0; }
.field input, .field select, .field textarea { font-weight: 400; font-size: 16px; color: var(--ink); }
.kind { display: flex; border: 1px solid var(--line); border-radius: 6px; padding: 3px; gap: 3px; }
.kind button { flex: 1; min-height: 40px; border: 0; background: none; font-size: 15px; font-weight: 550; color: var(--muted); }
.kind [aria-pressed="true"] { color: var(--accent-ink); background: var(--accent); }
.amount-field { position: relative; }
.amount-field input { font-size: 30px; font-weight: 600; font-variant-numeric: tabular-nums; height: 62px; padding-right: 44px; letter-spacing: -.02em; }
.amount-field > span { position: absolute; right: 15px; top: 50%; transform: translateY(-50%); font-weight: 500; color: var(--ink); font-size: 17px; }
.quick { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin: 0 -2px; padding: 2px; }
.quick::-webkit-scrollbar { display: none; }
.quick .chip { flex-shrink: 0; font-weight: 500; color: var(--ink); font-size: 14px; min-height: 36px; }
.quick .chip small { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.dates .chip { padding: 7px 12px; }
.dates input[type="date"] { width: auto; flex: 1 1 124px; min-height: 40px; padding: 6px 10px; font-size: 15px; border-radius: 999px; }
.dates input.other { border-color: var(--warn); color: var(--warn); font-weight: 600; }
.categories .chip, .meals .chip { color: var(--ink); font-weight: 500; }
.chips .chip[aria-pressed="true"], .chips .chip:has(input:checked) { color: var(--accent-ink); }
.field-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.field-head .text-button { color: var(--accent-text); text-decoration: none; font-weight: 600; padding: 6px 0; }
.field small { font-weight: 400; font-size: 12px; margin-left: 4px; }
.people-state { color: var(--accent-text); font-size: 13px; margin-left: 4px; }
.chip.preset { border-color: var(--accent); color: var(--accent-text); }
.chip.preset.editing { gap: 2px; padding-right: 4px; cursor: default; }
.chip .mini { border: 0; background: none; color: inherit; min-width: 30px; min-height: 30px; padding: 0; border-radius: 999px; font-size: 15px; }
.chip .mini:hover { background: var(--surface); }
.payments .chip, .places .chip, .people .chip { color: var(--ink); font-weight: 500; }
.more-chip { color: var(--muted); border-style: dashed; }
.together-note { margin-top: -8px; }
details summary { cursor: pointer; display: flex; align-items: center; gap: 8px; padding: 6px 0; list-style: none; font-size: 14px; color: var(--muted); }
details summary::-webkit-details-marker { display: none; }
details[open] summary svg { transform: rotate(90deg); }
.details-fields { display: flex; flex-direction: column; gap: 16px; padding-top: 14px; }

/* Lists */
.day-heading { display: flex; justify-content: space-between; gap: 12px; font-size: 13px; font-weight: 550; color: var(--muted); margin-top: 22px; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
.day-heading span:last-child { font-variant-numeric: tabular-nums; font-weight: 500; }
.record { width: 100%; display: flex; align-items: center; gap: 12px; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: none; border-radius: 0; padding: 12px 2px; min-height: 62px; white-space: normal; }
.record:hover { background: var(--surface); }
.record-text { flex: 1; min-width: 0; }
.record-title { display: block; font-size: 16px; font-weight: 550; overflow-wrap: anywhere; }
.record-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 6px; font-size: 13px; color: var(--muted); margin-top: 2px; }
.record-meta > .m ~ .m::before { content: "·"; margin-right: 6px; }
.record-note { display: block; font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 1px; }
.badge { display: inline-block; padding: 0 7px; border-radius: 999px; background: var(--accent-soft); color: var(--accent-text); font-size: 12px; font-weight: 600; line-height: 20px; }
.badge.person { background: var(--surface); color: var(--ink); border: 1px solid var(--line); line-height: 18px; }
.record-amount { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.record-amount.income { color: var(--accent-text); }
.record-date { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 38px; }
.recent .record { min-height: 56px; padding: 10px 2px; }

/* History */
.month-nav { display: flex; align-items: center; gap: 10px; padding-top: 24px; }
.month-label { font-size: 22px; font-weight: 700; letter-spacing: -.03em; min-width: 132px; text-align: center; font-variant-numeric: tabular-nums; }
.month-total { margin: 10px 0 16px; font-size: 15px; font-variant-numeric: tabular-nums; color: var(--muted); }
.month-total strong { color: var(--ink); font-weight: 650; }
.search { display: flex; position: relative; align-items: center; }
.search svg { position: absolute; left: 14px; color: var(--muted); pointer-events: none; }
.search input { padding-left: 44px; }
.filters { display: flex; gap: 8px; margin: 12px 0 4px; flex-wrap: wrap; }
.filters .chip { font-size: 14px; min-height: 36px; }
#load-more { margin-top: 20px; }
#view-history { max-width: 720px; }

/* Stats */
#stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; margin-top: 16px; }
#stats > .empty { grid-column: 1 / -1; }
.card { border: 1px solid var(--line); border-radius: 8px; padding: 18px; min-width: 0; }
.card.full { grid-column: 1 / -1; }
.card h3 { font-size: 15px; font-weight: 650; display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.card h3 small { font-size: 12px; font-weight: 500; color: var(--muted); }
.big { font-size: 26px; font-weight: 700; letter-spacing: -.03em; font-variant-numeric: tabular-nums; margin-top: 8px; }
.big small { font-size: 14px; font-weight: 500; color: var(--muted); letter-spacing: 0; }
.card .bar { margin: 12px 0 10px; height: 10px; }
.card-lines { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.card-lines .over { color: var(--error); font-weight: 600; }
.card-actions { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; align-items: center; }
.card-actions .secondary { min-height: 38px; padding: 6px 12px; font-size: 14px; }
.budget-form { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; font-size: 13px; color: var(--muted); }
.budget-form .amount-field input { font-size: 18px; height: 46px; font-weight: 500; }
.budget-form .row { display: flex; gap: 8px; }
.budget-form .row button { flex: 1; min-height: 40px; font-size: 14px; }
.budget-form .primary { min-height: 40px; font-size: 14px; padding: 6px 12px; }
.compare { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 14px; font-variant-numeric: tabular-nums; }
.compare th, .compare td { padding: 9px 0; border-bottom: 1px solid var(--line); text-align: right; }
.compare th:first-child, .compare td:first-child { text-align: left; }
.compare th { font-size: 12px; font-weight: 500; color: var(--muted); }
.compare .up { color: var(--error); }
.compare .down { color: var(--accent-text); }
.cat-list { list-style: none; margin: 10px 0 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.cat-row { display: grid; grid-template-columns: minmax(56px, 96px) minmax(0, 1fr) 104px; gap: 4px 12px; align-items: center; font-size: 14px; }
.cat-row .bar { margin: 0; }
.cat-row .amount { font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; }
.cat-row .prev { grid-column: 2 / -1; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.cat-group { margin-top: 18px; font-size: 12px; color: var(--muted); font-weight: 600; }

/* Toast and dialog */
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 30; width: min(560px, calc(100% - 32px)); display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 16px; background: var(--ink); color: var(--bg); border-radius: 8px; font-size: 14px; }
.toast button { background: none; border: 0; padding: 8px; text-decoration: underline; color: inherit; font-weight: 650; }
.toast.error { background: var(--error); color: #ffffff; }
dialog { color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 24px; max-height: calc(100dvh - 32px); width: min(480px, calc(100% - 24px)); overflow-y: auto; }
dialog::backdrop { background: var(--veil); }
.dialog-heading { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-bottom: 20px; }
.dialog-heading button { font-size: 14px; }
.delete-area { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--line); font-size: 14px; }
.danger-text { background: none; border: 0; padding: 10px 0; color: var(--error); }
.danger { border: 1px solid var(--error); background: var(--error); color: var(--bg); padding: 10px 16px; }
.confirmation { display: flex; gap: 10px; margin-top: 12px; }
#edit-error { margin-top: 16px; }

@media (max-width: 900px) {
  .record-layout { grid-template-columns: minmax(0, 1fr); gap: 36px; }
  .composer { max-width: 560px; }
}
@media (max-width: 760px) {
  body { padding: 0 16px calc(var(--tabbar) + env(safe-area-inset-bottom) + 32px); }
  .header { padding: 12px 0; gap: 12px; }
  h1 { font-size: 22px; }
  .tabs { position: fixed; left: 0; right: 0; bottom: 0; z-index: 25; margin: 0; gap: 0; padding: 0 8px env(safe-area-inset-bottom); background: var(--bg); border-top: 1px solid var(--line); }
  .tabs a { flex: 1; flex-direction: column; justify-content: center; gap: 2px; height: var(--tabbar); padding: 0; border-radius: 0; font-size: 12px; }
  .tabs a[aria-current="page"] { background: none; }
  .tabs a svg { width: 22px; height: 22px; }
  .menu { margin-left: auto; }
  .summary { padding: 14px 0; gap: 4px 16px; }
  .summary-week { flex-basis: 100%; font-size: 13px; }
  .summary-values strong { font-size: 19px; }
  .record-layout { padding-top: 18px; }
  .recent { border-top: 1px solid var(--line); padding-top: 22px; }
  .month-nav { padding-top: 16px; }
  .month-label { font-size: 20px; min-width: 0; flex: 1; }
  #stats { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .card { padding: 16px; }
  .toast { bottom: calc(var(--tabbar) + env(safe-area-inset-bottom) + 12px); }
  dialog { margin: auto 0 0; width: 100%; max-width: 100%; max-height: 92dvh; border-radius: 14px 14px 0 0; border-bottom: 0; padding: 20px 16px calc(20px + env(safe-area-inset-bottom)); }
}
@media (max-width: 359px) {
  body { padding-left: 12px; padding-right: 12px; }
  .summary-values { gap: 14px; }
  .summary-values strong { font-size: 17px; }
  .chip { padding: 6px 11px; font-size: 14px; }
  .amount-field input { font-size: 26px; }
}
