:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #667085;
  --border: #e3e6eb;
  --accent: #1a9c75;
  --accent-text: #ffffff;
  --accent-soft: #e5f6f0;
  --danger: #c4372b;
  --danger-soft: #fdecea;
  --pos: #11845b;
  --neg: #c4372b;
  --warn-soft: #fff6e0;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c;
    --surface: #1a2029;
    --text: #e6e9ee;
    --muted: #98a2b3;
    --border: #2b3340;
    --accent: #2fbf91;
    --accent-text: #0b1410;
    --accent-soft: #173229;
    --danger: #f07167;
    --danger-soft: #3a1f1d;
    --pos: #4ad3a0;
    --neg: #f07167;
    --warn-soft: #3a3120;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .6rem; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .8rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .inner {
  max-width: 1040px; margin: 0 auto; padding: .7rem 16px;
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
}
.brand { font-weight: 700; color: var(--text); text-decoration: none; font-size: 1.05rem; }
.brand span { color: var(--accent); }
.nav { display: flex; gap: .9rem; align-items: center; margin-left: auto; flex-wrap: wrap; }
.nav a { color: var(--muted); text-decoration: none; }
.nav a:hover, .nav a.active { color: var(--text); }

main { max-width: 1040px; margin: 0 auto; padding: 1.2rem 16px 3rem; }

.grid { display: grid; gap: 1rem; grid-template-columns: 1fr; }
@media (min-width: 860px) {
  .grid.two { grid-template-columns: 3fr 2fr; align-items: start; }
}
.stack > * + * { margin-top: 1rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  box-shadow: var(--shadow);
  min-width: 0;
}
.card > h2:first-child { margin-bottom: .8rem; }

.flash { padding: .6rem .9rem; border-radius: var(--radius); margin-bottom: 1rem; border: 1px solid var(--border); background: var(--surface); }
.flash.success { background: var(--accent-soft); border-color: transparent; }
.flash.error { background: var(--danger-soft); border-color: transparent; }
.flash.warn { background: var(--warn-soft); border-color: transparent; }

label { display: block; font-weight: 500; margin-bottom: .2rem; font-size: .9rem; }
input[type=text], input[type=password], input[type=date], input[type=number], select, textarea {
  width: 100%;
  padding: .5rem .6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { min-height: 4.5rem; resize: vertical; }
.field { margin-bottom: .8rem; }
.row { display: flex; gap: .6rem; flex-wrap: wrap; }
.row > .field { flex: 1 1 140px; }
.check { display: flex; align-items: center; gap: .4rem; font-weight: 400; }
.checks { display: flex; flex-wrap: wrap; gap: .3rem .9rem; }

button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .3rem;
  padding: .5rem .95rem;
  border-radius: 8px;
  border: 1px solid transparent;
  background: var(--accent);
  color: var(--accent-text);
  font: inherit; font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
button:hover, .btn:hover { filter: brightness(1.05); }
button.secondary, .btn.secondary { background: transparent; color: var(--text); border-color: var(--border); }
button.danger { background: transparent; color: var(--danger); border-color: var(--border); }
button.link { background: none; border: 0; padding: 0; color: var(--accent); font-weight: 500; }
button.small, .btn.small { padding: .25rem .6rem; font-size: .85rem; }
form.inline { display: inline; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .5rem .4rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: 0; }
.table-wrap { overflow-x: auto; }

.list { list-style: none; margin: 0; padding: 0; }
.list > li { padding: .65rem 0; border-bottom: 1px solid var(--border); }
.list > li:last-child { border-bottom: 0; }

.balance-row { display: flex; justify-content: space-between; gap: .6rem; padding: .35rem 0; }
.transfer { display: flex; justify-content: space-between; align-items: center; gap: .6rem; padding: .45rem 0; flex-wrap: wrap; }

.expense { display: grid; grid-template-columns: 3.4rem 1fr auto; gap: .2rem .8rem; align-items: start; }
.expense .date { color: var(--muted); font-size: .8rem; text-align: center; line-height: 1.2; padding-top: .15rem; }
.expense .date b { display: block; font-size: 1.05rem; color: var(--text); }
.expense .title { font-weight: 600; overflow-wrap: anywhere; }
.expense .amount { text-align: right; }
.expense .actions { grid-column: 2 / -1; display: flex; gap: .5rem; flex-wrap: wrap; }
.expense.deleted { opacity: .6; }
.expense.deleted .title { text-decoration: line-through; }
.tag { display: inline-block; font-size: .72rem; padding: .05rem .45rem; border-radius: 99px; background: var(--accent-soft); color: var(--text); font-weight: 600; vertical-align: middle; }
.tag.grey { background: var(--border); }

.share-link { display: flex; gap: .5rem; }
.share-link input { font-family: ui-monospace, monospace; font-size: .85rem; }

.stats { display: flex; gap: 1.5rem; flex-wrap: wrap; }
.stats div b { display: block; font-size: 1.25rem; }

.pager { display: flex; justify-content: space-between; margin-top: 1rem; }
details summary { cursor: pointer; color: var(--muted); }
pre.diff { white-space: pre-wrap; overflow-wrap: anywhere; font-size: .8rem; background: var(--bg); padding: .5rem; border-radius: 6px; margin: .3rem 0 0; }
.fx-preview { min-height: 1.3rem; }
.center-card { max-width: 420px; margin: 3rem auto; }
.who-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .6rem; }
.who-grid button { width: 100%; padding: .8rem; }

@media (max-width: 560px) {
  .expense { grid-template-columns: 2.6rem 1fr; }
  .expense .amount { grid-column: 2; text-align: left; display: flex; gap: .6rem; flex-wrap: wrap; align-items: baseline; }
  .stats { gap: .8rem 1.5rem; }
}

/* --- Split mode toggle and itemised bills --- */
.split-mode { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; margin-bottom: .8rem; }
.split-mode .seg { margin: 0; padding: .4rem .85rem; cursor: pointer; font-weight: 500; display: flex; align-items: center; gap: .35rem; }
.split-mode .seg + .seg { border-left: 1px solid var(--border); }
.split-mode .seg:has(input:checked) { background: var(--accent-soft); }
.split-mode input { accent-color: var(--accent); }
form:has(input[name=split_mode][value=items]:checked) .equal-only { display: none; }
form:has(input[name=split_mode][value=equal]:checked) .items-only { display: none; }

.items-table { display: grid; gap: .45rem; margin-bottom: .5rem; }
.items-head, .item-row { display: grid; grid-template-columns: minmax(0, 2.2fr) 6.5rem minmax(0, 3fr) 1.4rem; gap: .5rem; align-items: center; }
.items-head { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
.who-head { display: flex; flex-wrap: wrap; gap: .3rem; align-items: center; text-transform: none; letter-spacing: 0; }
.item-row input[type=text] { padding: .4rem .5rem; }
.chips { display: flex; flex-wrap: wrap; gap: .3rem; }
.chip { margin: 0; font-weight: 500; font-size: .8rem; cursor: pointer; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span { display: inline-block; padding: .2rem .55rem; border-radius: 99px; border: 1px solid var(--border); color: var(--muted); user-select: none; }
.chip input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }
.chip-btn { background: transparent; color: var(--text); border: 1px dashed var(--border); padding: .1rem .5rem; font-size: .78rem; font-weight: 500; border-radius: 99px; }
.split-preview { margin: .2rem 0 .9rem; min-height: 1.2rem; }
.split-summary { display: flex; flex-wrap: wrap; gap: .3rem .9rem; }
@media (max-width: 640px) {
  .items-head > span:not(.who-head) { display: none; }
  .items-head { grid-template-columns: 1fr; }
  .item-row { grid-template-columns: minmax(0, 1fr) 6rem 1.4rem; padding: .5rem 0; border-bottom: 1px solid var(--border); }
  .item-row .chips { grid-column: 1 / -1; grid-row: 2; }
}
.item-breakdown { margin-top: .25rem; }
.item-breakdown table { margin-top: .3rem; }
.item-breakdown td { padding: .2rem .3rem; }
.item-breakdown .share-row td { border-top: 1px solid var(--border); }
.item-breakdown .share-row + .share-row td { border-top: 0; }

/* --- Receipt scanning --- */
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.scan-bar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin-bottom: 1rem; padding-bottom: 1rem; border-bottom: 1px dashed var(--border); }
.scan-bar label.btn { font-weight: 600; margin: 0; }
.scan-bar label.btn:focus-within { outline: 2px solid var(--accent); outline-offset: 1px; }
.scan-result { display: flex; gap: .8rem; align-items: flex-start; }
.receipt-thumb { width: 64px; height: 86px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); flex: none; }
.scanning { opacity: .55; pointer-events: none; }

/* --- Share QR code --- */
.qr-dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 1.2rem; width: min(92vw, 380px); text-align: center; }
.qr-dialog::backdrop { background: rgba(10, 14, 20, .55); }
.qr-box { background: #fff; border-radius: 8px; padding: .4rem; margin: .4rem auto .6rem; max-width: 300px; }
.qr-svg { display: block; width: 100%; height: auto; }
.qr-url { overflow-wrap: anywhere; color: var(--muted); }
