:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #eef0ec;
  --text: #1c2321;
  --muted: #65706b;
  --border: #dde1dc;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #d6efeb;
  --ok: #15803d;   --ok-soft: #dcf3e3;
  --warn: #b45309; --warn-soft: #fbecd5;
  --bad: #b91c1c;  --bad-soft: #fbe0e0;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 4px 14px rgb(0 0 0 / .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111614; --surface: #1a201e; --surface-2: #232a27; --text: #e6ebe8; --muted: #93a09a;
    --border: #2e3633; --accent: #2dd4bf; --accent-ink: #062b27; --accent-soft: #123a35;
    --ok: #4ade80; --ok-soft: #13301f; --warn: #fbbf24; --warn-soft: #3a2c10; --bad: #f87171; --bad-soft: #3d1717;
    --shadow: none; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.1rem; }
h3 { font-size: .95rem; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.ok-text { color: var(--ok); } .warn-text { color: var(--warn); } .bad-text { color: var(--bad); }

/* Top bar + tabs (tabs become a bottom bar on phones) */
.topbar { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 14px 16px 8px;
  display: flex; align-items: center; gap: 16px; max-width: 860px; margin: 0 auto; }
.topbar h1 { font-size: 1.25rem; letter-spacing: -.01em; }
.tabs { display: flex; gap: 4px; margin-left: auto; background: var(--surface-2); padding: 4px; border-radius: 999px; }
.tabs button { border: 0; background: none; color: var(--muted); font: inherit; font-weight: 600;
  padding: 7px 16px; border-radius: 999px; cursor: pointer; }
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
@media (max-width: 640px) {
  .tabs { position: fixed; left: 12px; right: 12px; bottom: calc(10px + env(safe-area-inset-bottom));
    margin: 0; justify-content: space-around; box-shadow: 0 4px 20px rgb(0 0 0 / .12); border: 1px solid var(--border); }
  .tabs button { flex: 1; padding: 11px 0; }
}

main { max-width: 860px; margin: 0 auto; padding: 4px 16px 16px; }
.view { display: grid; gap: 12px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); display: grid; gap: 10px; }

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 600; padding: 9px 16px;
  border-radius: 10px; cursor: pointer; text-decoration: none; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); }
.btn.big { padding: 16px; font-size: 1rem; flex: 1; }
.icon-btn { border: 0; background: var(--surface-2); color: var(--text); width: 36px; height: 36px; border-radius: 10px;
  font-size: 1.1rem; cursor: pointer; flex: none; }

/* Upload */
.upload-actions { display: flex; gap: 10px; }
@media (max-width: 400px) { .upload-actions { flex-direction: column; } }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; }
.row { display: flex; gap: 12px; align-items: center; padding: 11px 2px; border-top: 1px solid var(--border); }
.row:first-child { border-top: 0; }
.row.clickable { cursor: pointer; }
.row.clickable:hover { background: var(--surface-2); border-radius: 8px; }
.row-main { display: grid; min-width: 0; flex: 1; }
.row-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { color: var(--muted); font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-end { display: grid; justify-items: end; gap: 3px; flex: none; }
.amount { font-weight: 600; font-variant-numeric: tabular-nums; }
.empty { padding: 16px 0; text-align: center; }
.pill { font-size: .72rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.busy { background: var(--accent-soft); color: var(--accent); }

/* Spending */
.month-bar { display: flex; gap: 8px; align-items: center; }
.month-bar input { flex: 1; text-align: center; font-weight: 600; }
.period { text-align: center; margin: 0; font-size: .9rem; }
.totals { display: grid; justify-items: center; gap: 2px; padding-top: 4px; }
.big-num { font-size: 1.9rem; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.list-head { display: flex; align-items: center; gap: 10px; }
.list-head select { margin-left: auto; width: auto; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 30%) 1fr auto; gap: 10px; align-items: center; font-size: .9rem; }
.bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar-fill { display: block; height: 100%; background: var(--accent); border-radius: 999px; }
.bar-value { font-variant-numeric: tabular-nums; font-weight: 600; }

/* Forms */
label { display: grid; gap: 4px; font-size: .85rem; font-weight: 600; color: var(--muted); }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 9px 11px; width: 100%; font-weight: 400; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.settings { gap: 14px; }
.settings h2 { margin-top: 6px; }
.settings small { font-weight: 400; }
.inline-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.form-actions { display: flex; justify-content: flex-end; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* Editor dialog */
dialog.editor { border: 0; padding: 0; border-radius: var(--radius); background: var(--surface); color: var(--text);
  width: min(820px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); }
dialog.editor::backdrop { background: rgb(0 0 0 / .45); }
.editor-body { padding: 16px; display: grid; gap: 12px; }
.editor-head { display: flex; align-items: center; justify-content: space-between; }
.editor-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 640px) { .editor-grid { grid-template-columns: 1fr; } }
.preview { background: var(--surface-2); border-radius: 10px; display: grid; place-items: center; min-height: 120px; overflow: hidden; }
.preview img { display: block; width: 100%; max-height: 60vh; object-fit: contain; }
.fields { display: grid; gap: 10px; align-content: start; }
.editor-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.banner { margin: 0; padding: 8px 12px; border-radius: 10px; font-size: .9rem; }
.banner.warn { background: var(--warn-soft); color: var(--warn); }
.banner.bad { background: var(--bad-soft); color: var(--bad); }
.items summary { cursor: pointer; font-size: .85rem; color: var(--muted); font-weight: 600; }
.items table { width: 100%; border-collapse: collapse; font-size: .85rem; margin-top: 6px; }
.items td { padding: 4px 0; border-top: 1px solid var(--border); }
.num { text-align: right; white-space: nowrap; padding-left: 8px; font-variant-numeric: tabular-nums; }
.error { color: var(--bad); }

/* Toast */
.toast { position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: .9rem; opacity: 0;
  pointer-events: none; transition: .2s; max-width: calc(100vw - 32px); z-index: 20; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast[data-kind="bad"] { background: var(--bad); color: #fff; }
.toast[data-kind="warn"] { background: var(--warn); color: #fff; }
@media (min-width: 641px) { .toast { bottom: 24px; } }
.btn.small { padding: 4px 10px; font-size: .8rem; }

/* Trends */
.segmented { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.segmented button { flex: 1; border: 0; background: none; color: var(--muted); font: inherit; font-weight: 600;
  padding: 8px; border-radius: 8px; cursor: pointer; }
.segmented button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { display: grid; gap: 2px; align-content: start; }
.stat-label { font-size: .75rem; color: var(--muted); font-weight: 600; }
.stat-value { font-size: 1.1rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.stat-sub { font-size: .75rem; color: var(--muted); }
@media (max-width: 420px) { .stat-value { font-size: .88rem; } .stats { gap: 6px; } }
.stat-value { white-space: nowrap; }

.chart-plot { position: relative; height: 220px; margin: 6px 0 34px 34px; }
.chart-grid { position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: space-between; pointer-events: none; }
.grid-line { border-top: 1px solid var(--border); height: 0; position: relative; }
.grid-line span { position: absolute; right: calc(100% + 6px); top: -.6em; font-size: .7rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.avg-line { position: absolute; left: 0; right: 0; border-top: 1.5px dashed var(--muted); pointer-events: none; z-index: 1; }
.avg-line span { position: absolute; right: 0; top: -1.25em; font-size: .68rem; color: var(--muted); background: var(--surface); padding: 0 3px; }
.bars { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(var(--cols), 1fr); gap: 2px; }
.bar-col { position: relative; display: flex; align-items: flex-end; justify-content: center; border: 0; background: none;
  padding: 0; cursor: pointer; height: 100%; }
.bar-col:hover, .bar-col:focus-visible { background: color-mix(in srgb, var(--text) 5%, transparent); border-radius: 6px; outline: none; }
.bar { width: min(70%, 28px); background: var(--accent); border-radius: 4px 4px 0 0; min-height: 0; }
.bar.current { background: repeating-linear-gradient(135deg, var(--accent) 0 5px, color-mix(in srgb, var(--accent) 55%, var(--surface)) 5px 8px); }
.tip { position: absolute; bottom: calc(100% + 4px); left: 50%; transform: translateX(-50%); display: none; z-index: 3;
  background: var(--text); color: var(--bg); padding: 7px 10px; border-radius: 8px; font-size: .78rem; white-space: nowrap;
  text-align: left; gap: 1px; box-shadow: 0 4px 14px rgb(0 0 0 / .2); }
.tip .muted { color: color-mix(in srgb, var(--bg) 70%, var(--text)); }
.bar-col:hover .tip, .bar-col:focus-visible .tip { display: grid; }
.bar-col:nth-child(-n+3) .tip { left: 0; transform: none; }
.bar-col:nth-last-child(-n+3) .tip { left: auto; right: 0; transform: none; }
.x-labels { position: absolute; left: 0; right: 0; top: calc(100% + 6px); display: grid; grid-template-columns: repeat(var(--cols), 1fr);
  gap: 2px; text-align: center; font-size: .7rem; color: var(--muted); line-height: 1.15; }
.x-labels small { font-size: .65rem; }
.table-view summary { cursor: pointer; font-size: .85rem; color: var(--muted); font-weight: 600; }
.table-view table { width: 100%; border-collapse: collapse; font-size: .85rem; margin-top: 6px; }
.table-view th { text-align: left; color: var(--muted); font-weight: 600; padding: 4px 0; }
.table-view td { padding: 5px 0; border-top: 1px solid var(--border); }

/* Foreign currency block in the editor */
.foreign { border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; }
.foreign summary { cursor: pointer; font-size: .85rem; font-weight: 600; color: var(--muted); }
.foreign[open] { display: grid; gap: 10px; }
.foreign[open] summary { margin-bottom: 2px; }
.rate-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: end; }

.editor-grid.no-file { grid-template-columns: 1fr; }
.editor-grid.no-file .preview { min-height: 0; padding: 8px 12px; place-items: start; }
.editor-grid.no-file .preview p { margin: 0; }
