:root {
  --bg: #f6f3ef;
  --surface: #ffffff;
  --surface-2: #f1ece6;
  --ink: #1f1a17;
  --ink-2: #5c534c;
  --muted: #8a8079;
  --line: #e4ddd5;
  --accent: #8c1d2f;
  --accent-ink: #ffffff;
  --accent-soft: #f6e7e9;
  --good: #1d7a46;
  --good-soft: #e3f3e9;
  --warn: #9a5b00;
  --warn-soft: #fbefd9;
  --bad: #b3261e;
  --bad-soft: #fbe5e3;
  --bar: #8c1d2f;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(31, 26, 23, 0.06), 0 2px 8px rgba(31, 26, 23, 0.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151210;
    --surface: #1f1b18;
    --surface-2: #2a2521;
    --ink: #f3eee9;
    --ink-2: #c9c0b8;
    --muted: #978d85;
    --line: #362f2a;
    --accent: #e0697b;
    --accent-ink: #1a0b0e;
    --accent-soft: #3a1c22;
    --good: #5fcf8c;
    --good-soft: #17301f;
    --warn: #f0b456;
    --warn-soft: #362712;
    --bad: #ff8a80;
    --bad-soft: #3a1a18;
    --bar: #e0697b;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
body.noscroll { overflow: hidden; }
a { color: var(--accent); text-decoration: none; }
h1 { font-size: 22px; margin: 0; letter-spacing: -0.01em; }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 22px 0 8px; }
.card h3 { margin-top: 0; }
h4 { margin: 12px 0 6px; font-size: 14px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.pad { padding: 0 2px; }
.mono { font-family: ui-monospace, Menlo, monospace; font-size: 11px; word-break: break-all; }
.pre { white-space: pre-line; }
.tone-bad { color: var(--bad); }
.tone-warn { color: var(--warn); }
.tone-good { color: var(--good); }
.tone-muted { color: var(--muted); }

.screen { max-width: 720px; margin: 0 auto; padding: 0 16px calc(96px + env(safe-area-inset-bottom)); }
header.top {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 8px;
  padding: calc(14px + env(safe-area-inset-top)) 0 12px; margin: 0 -16px 8px; padding-left: 16px; padding-right: 16px;
  background: var(--bg);
}
header.top h1 { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* bottom tabs */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: flex; justify-content: space-around;
  background: var(--surface); border-top: 1px solid var(--line); padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
.tabs a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; color: var(--muted); font-size: 11px; padding: 4px 0; min-width: 0; }
.tabs a.on { color: var(--accent); font-weight: 600; }
.tabs a.add svg { background: var(--accent); color: var(--accent-ink); border-radius: 12px; padding: 6px; width: 38px; height: 38px; margin-top: -10px; box-shadow: var(--shadow); }

/* cards & lists */
.card { background: var(--surface); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); border: 1px solid var(--line); margin-bottom: 12px; }
.subcard { background: var(--surface-2); border-radius: var(--radius); padding: 12px; margin-bottom: 14px; }
.list { background: var(--surface); border-radius: var(--radius); border: 1px solid var(--line); overflow: hidden; margin-bottom: 12px; }
.list.flat { border: 0; border-radius: 0; margin: 0 -14px; }
.item {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line);
  color: var(--ink); background: none; width: 100%; text-align: left; font: inherit; border-left: 0; border-right: 0; border-top: 0;
}
.item:last-child { border-bottom: 0; }
a.item:active, button.item:active { background: var(--surface-2); }
.item.dim { opacity: 0.5; }
.item-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.item-main b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-main small, .item-side small { color: var(--ink-2); font-size: 12.5px; }
.item-side { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; flex-shrink: 0; }
.chev { color: var(--muted); font-size: 20px; }
.me { display: flex; flex-direction: column; }

.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.muted { background: var(--surface-2); color: var(--ink-2); }

.summary { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; color: var(--ink-2); margin: 4px 2px 10px; }
.empty { text-align: center; color: var(--muted); padding: 28px 12px; }
.errorbox { background: var(--bad-soft); color: var(--bad); border-radius: 10px; padding: 10px 12px; margin: 8px 0 12px; font-size: 14px; }
.loading { display: flex; justify-content: center; padding: 48px; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* forms */
.form { display: block; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; min-width: 0; }
.label { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.hint { font-size: 12px; color: var(--muted); }
.err { font-size: 12px; color: var(--bad); }
input, select, textarea, .picker {
  width: 100%; font: inherit; font-size: 16px; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; min-height: 46px; outline: none;
}
input:focus, select:focus, textarea:focus, .picker:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; }
input.mini { width: 92px; min-height: 40px; padding: 8px; text-align: right; flex-shrink: 0; }
.picker { text-align: left; display: flex; flex-direction: column; justify-content: center; cursor: pointer; }
.picker.placeholder span { color: var(--muted); }
.picker small { color: var(--ink-2); font-size: 12px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.total-line { text-align: right; margin: -6px 2px 14px; color: var(--ink-2); }
details.more-fields { margin-bottom: 14px; }
details.more-fields summary { cursor: pointer; color: var(--accent); font-weight: 600; padding: 6px 0 12px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 600;
  border-radius: 10px; padding: 11px 16px; min-height: 44px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--bad); }
.btn.ghost { border-style: dashed; color: var(--accent); background: none; }
.btn.block { width: 100%; }
.btn.big { min-height: 52px; font-size: 16px; margin-top: 4px; }
.btn.small { min-height: 34px; padding: 6px 12px; font-size: 13px; }
.btn:disabled { opacity: 0.55; }
.link { background: none; border: 0; color: var(--accent); font: inherit; font-weight: 600; cursor: pointer; padding: 6px; }
.link.danger { color: var(--bad); font-size: 13px; padding: 0; }
.icon-btn { background: none; border: 0; color: var(--ink); padding: 6px; margin-left: -6px; display: inline-flex; cursor: pointer; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }
.actions .btn { flex: 1 1 auto; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.chips.scroll { flex-wrap: nowrap; overflow-x: auto; margin: 0 -16px 10px; padding: 0 16px; scrollbar-width: none; }
.chip {
  font: inherit; font-size: 14px; padding: 8px 14px; border-radius: 99px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); cursor: pointer; white-space: nowrap;
}
.chip.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border-radius: 12px; padding: 4px; }
.seg button { font: inherit; font-weight: 600; border: 0; border-radius: 9px; padding: 10px; background: none; color: var(--ink-2); cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

.toggle { display: inline-flex; align-items: center; gap: 8px; background: none; border: 0; padding: 0; cursor: pointer; font: inherit; color: var(--ink); flex-shrink: 0; }
.toggle .knob { width: 46px; height: 28px; border-radius: 99px; background: var(--line); position: relative; transition: background 0.15s; }
.toggle .knob::after { content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.25); transition: transform 0.15s; }
.toggle.on .knob { background: var(--good); }
.toggle.on .knob::after { transform: translateX(18px); }

.search { display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; margin-bottom: 10px; color: var(--muted); }
.search input { border: 0; padding: 10px 0; box-shadow: none !important; background: none; }
.filters { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; margin-bottom: 10px; }
.filters select, .filters .picker { font-size: 13px; min-height: 40px; padding: 8px 10px; }

/* photos */
.photos { display: flex; flex-wrap: wrap; gap: 10px; }
.thumb { position: relative; width: 84px; height: 84px; border-radius: 10px; overflow: hidden; background: var(--surface-2); border: 1px solid var(--line); display: flex; align-items: center; justify-content: center; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb button { position: absolute; top: 4px; right: 4px; width: 24px; height: 24px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; display: flex; align-items: center; justify-content: center; padding: 0; cursor: pointer; }
.thumb.add { flex-direction: column; gap: 2px; color: var(--accent); font: inherit; font-size: 12px; font-weight: 600; border-style: dashed; cursor: pointer; }
.pdf { display: flex; flex-direction: column; align-items: center; font-size: 11px; font-weight: 700; color: var(--ink-2); }

/* bill detail */
.big-amount { font-size: 28px; font-weight: 700; display: flex; align-items: center; gap: 10px; letter-spacing: -0.01em; }
.vendor-link { font-size: 17px; font-weight: 600; display: inline-block; margin: 2px 0 4px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 12px 0 0; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.saved { display: flex; gap: 10px; align-items: flex-start; background: var(--good-soft); border-color: transparent; color: var(--ink); }
.saved svg { color: var(--good); flex-shrink: 0; }

/* pay */
.pay-item.on { background: var(--accent-soft); }
.check { width: 24px; height: 24px; border-radius: 7px; border: 2px solid var(--line); background: var(--surface); flex-shrink: 0; cursor: pointer; padding: 0; }
.check.on { background: var(--accent); border-color: var(--accent); box-shadow: inset 0 0 0 3px var(--surface); }

/* tables */
.table-wrap { overflow-x: auto; margin: 0 -14px; padding: 0 14px; }
.card + .table-wrap, h3 + .table-wrap { margin: 0 0 12px; padding: 0; background: var(--surface); border-radius: var(--radius); border: 1px solid var(--line); }
.table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
.table th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table td { padding: 9px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table tr:last-child td { border-bottom: 0; }
.num { text-align: right; }

/* dashboard */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.tiles.three { grid-template-columns: 1fr 1fr 1fr; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: flex; flex-direction: column; gap: 2px; color: var(--ink); box-shadow: var(--shadow); min-width: 0; }
.tile-label { font-size: 12px; color: var(--ink-2); }
.tile-value { font-size: 22px; font-weight: 700; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; }
.tile-sub { font-size: 12px; color: var(--muted); }
.tile.strong { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.tile.strong .tile-label { color: inherit; opacity: 0.75; }
.tile.bad .tile-value { color: var(--bad); }
.tile.warn .tile-value { color: var(--warn); }
.range { margin-top: 8px; }
.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row { border-radius: 8px; }
.bar-row.clickable { cursor: pointer; }
.bar-row.on .bar-label span { font-weight: 700; }
.bar-label { display: flex; justify-content: space-between; gap: 10px; font-size: 13px; margin-bottom: 4px; }
.bar-label span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-label b { font-variant-numeric: tabular-nums; }
.bar-track { height: 8px; background: var(--surface-2); border-radius: 4px; }
.bar-fill { height: 100%; background: var(--bar); border-radius: 0 4px 4px 0; }
.subbars { margin-top: 14px; padding-top: 4px; border-top: 1px solid var(--line); }
.subbars .bar-fill { opacity: 0.6; }

/* categories */
.cat .subs { margin-top: 8px; padding-left: 4px; }
.cat .sub { padding: 6px 0; font-size: 14px; }
.cat .dim, .cat.dim { opacity: 0.5; }
.cat b, .cat .sub span { cursor: pointer; }

/* sheet */
.sheet-wrap { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.4); display: flex; align-items: flex-end; justify-content: center; }
.sheet { background: var(--bg); width: 100%; max-width: 640px; max-height: 92vh; border-radius: 18px 18px 0 0; display: flex; flex-direction: column; animation: up 0.18s ease-out; }
@keyframes up { from { transform: translateY(30px); opacity: 0.5; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; }
.sheet-head h2 { margin: 0; font-size: 18px; }
.sheet-body { overflow-y: auto; padding: 8px 16px calc(20px + env(safe-area-inset-bottom)); }
.pick-row { display: flex; flex-direction: column; width: 100%; text-align: left; font: inherit; color: var(--ink); background: var(--surface); border: 0; border-bottom: 1px solid var(--line); padding: 13px 14px; cursor: pointer; }
.pick-row:first-of-type { border-top-left-radius: 12px; border-top-right-radius: 12px; }
.pick-row:last-child { border-bottom: 0; border-bottom-left-radius: 12px; border-bottom-right-radius: 12px; }
.pick-row small { color: var(--ink-2); font-size: 12px; }
.pick-row.on { background: var(--accent-soft); font-weight: 600; }
.pick-row.add { color: var(--accent); font-weight: 600; border-radius: 12px; margin-bottom: 10px; border: 1px dashed var(--accent); }

.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 60; background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; max-width: calc(100% - 32px); box-shadow: 0 4px 16px rgba(0,0,0,.2); }
.toast.bad { background: var(--bad); color: #fff; }

/* login */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login .card { width: 100%; max-width: 380px; padding: 24px; }
.brand { font-size: 24px; font-weight: 800; margin-bottom: 18px; letter-spacing: -0.02em; }
.brand span { color: var(--accent); margin-left: 6px; font-weight: 600; }

@media (max-width: 400px) {
  .filters { grid-template-columns: 1fr 1fr; }
  .filters > :first-child { grid-column: 1 / -1; }
  .tile-value { font-size: 19px; }
}
