:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #16181d;
  --muted: #646b78;
  --border: #e2e5ea;
  --accent: #0f766e;
  --accent-soft: #e6f4f2;
  --accent-text: #ffffff;
  --credit: #15803d;
  --danger: #b42318;
  --warn-bg: #fff4e5;
  --warn-text: #93530a;
  --series-1: #2a78d6;      /* validated pair (dataviz palette slots 1–2) */
  --series-2: #eb6834;
  --context: #b9bdc5;       /* de-emphasised marks */
  --grid: #eceef1;
  --status-up: #b42318;     /* spending went up */
  --status-down: #15803d;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 6%);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --border: #2a2f38;
    --accent: #2dd4bf;
    --accent-soft: #11302d;
    --accent-text: #06201d;
    --credit: #4ade80;
    --danger: #f97066;
    --warn-bg: #3a2a12;
    --warn-text: #fbbf6a;
    --shadow: none;
    --series-1: #3987e5;
    --series-2: #d95926;
    --context: #4a515c;
    --grid: #252a32;
    --status-up: #f97066;
    --status-down: #4ade80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px));
}
a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.2; }

/* ── Top bar / navigation ─────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); font-size: 18px; }
.me {
  margin-left: auto; padding: 4px 12px; border-radius: 999px;
  background: var(--accent-soft); font-weight: 600; font-size: 14px;
}
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom, 0px));
  background: var(--surface); border-top: 1px solid var(--border);
}
.nav a {
  position: relative; display: flex; flex-direction: column; align-items: center;
  min-width: 96px; padding: 4px 12px; color: var(--muted); font-size: 12px; font-weight: 600;
  border-radius: 10px;
}
.nav a.active { color: var(--accent); }
.nav-icon { font-size: 20px; line-height: 1.1; }
.badge {
  position: absolute; top: 0; right: 22px;
  min-width: 18px; padding: 0 5px; border-radius: 999px;
  background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center;
}

@media (min-width: 760px) {
  body { padding-bottom: 0; }
  .nav { position: static; border: 0; padding: 0; gap: 4px; justify-content: flex-start; }
  .nav a { flex-direction: row; gap: 6px; min-width: 0; font-size: 14px; padding: 6px 12px; }
  .nav a.active { background: var(--accent-soft); }
  .nav-icon { font-size: 15px; }
  .badge { position: static; }
}

/* ── Layout ───────────────────────────────────────────────────────── */
.page { max-width: 1120px; margin: 0 auto; padding: 16px; }
.narrow { max-width: 560px; margin: 0 auto; }
.layout { display: grid; gap: 24px; }
@media (min-width: 960px) {
  .layout { grid-template-columns: minmax(360px, 420px) 1fr; align-items: start; }
  .layout-form { position: sticky; top: 76px; }
}
.section-title { font-size: 20px; margin-bottom: 12px; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px;
}
.muted, .note { color: var(--muted); font-size: 14px; }
.back { display: inline-block; margin-bottom: 8px; font-weight: 600; }
.empty { color: var(--muted); text-align: center; padding: 32px 0; }

/* ── Form ─────────────────────────────────────────────────────────── */
.expense-form { display: grid; gap: 14px; }
.row { display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: end; }
.field { display: grid; gap: 6px; margin: 0; padding: 0; border: 0; min-width: 0; }
.label { font-size: 13px; font-weight: 600; color: var(--muted); padding: 0; }
input, select, button { font: inherit; color: inherit; }
input[type="text"], input:not([type]), input[type="date"], select {
  width: 100%; min-height: 46px; padding: 10px 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
}
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.amount-input {
  display: flex; align-items: center; gap: 6px;
  min-height: 54px; padding: 0 14px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
  font-size: 26px; font-weight: 700;
}
.amount-input:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.amount-input input {
  border: 0; outline: 0; padding: 0; min-height: 0; width: 100%;
  font-size: 26px; font-weight: 700; background: transparent;
}
.segmented { display: flex; flex-wrap: wrap; gap: 0; }
.segmented legend { width: 100%; margin-bottom: 6px; }
.segmented label { flex: 1; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: block; min-height: 46px; padding: 11px 14px; text-align: center; white-space: nowrap;
  border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 600;
}
.segmented label:first-of-type span { border-radius: 10px 0 0 10px; }
.segmented label:last-of-type span { border-radius: 0 10px 10px 0; }
.segmented label + label span { border-left: 0; }
.segmented input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.row-amount .segmented { min-width: 170px; }
.field-date { min-width: 150px; }
@media (max-width: 480px) {
  .row-who { grid-template-columns: 1fr; }   /* payer buttons + date don't fit side by side */
}

.guess-tag {
  margin-left: 6px; padding: 1px 8px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); font-size: 11px; font-weight: 700;
}
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips:empty { display: none; }
.chip {
  padding: 6px 12px; min-height: 34px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); font-size: 14px; font-weight: 600;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.locked { margin: 0; font-weight: 600; }
.locked small { font-weight: 400; color: var(--muted); }

.has-error input, .has-error select, .has-error .amount-input, .has-error .segmented span { border-color: var(--danger); }
.error, .form-error { color: var(--danger); font-size: 13px; font-weight: 600; margin: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 10px 18px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); font-weight: 700; color: var(--text);
}
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); width: 100%; font-size: 17px; }
.btn-danger { color: var(--danger); }
.btn-big { width: 100%; min-height: 64px; font-size: 18px; }
.link { border: 0; background: none; color: var(--accent); font-weight: 700; cursor: pointer; padding: 4px; }
.actions { display: flex; gap: 12px; justify-content: space-between; margin-top: 16px; flex-wrap: wrap; }

/* ── Month list ───────────────────────────────────────────────────── */
.month-bar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.month-bar h2 { font-size: 20px; }
.icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); font-size: 22px; color: var(--text);
}
.summary { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.summary > div { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; display: grid; }
.summary strong { font-size: 20px; font-variant-numeric: tabular-nums; }
.filters { display: grid; grid-template-columns: 2fr 1fr; gap: 8px; margin-bottom: 12px; }
.expenses { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.expenses li + li { border-top: 1px solid var(--border); }
.expense { display: flex; align-items: center; gap: 12px; padding: 12px 14px; color: var(--text); }
.expense:hover { background: var(--bg); }
.avatar {
  flex: none; display: grid; place-items: center; width: 32px; height: 32px; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent); font-weight: 700; font-size: 14px;
}
.expense-main { display: grid; min-width: 0; flex: 1; }
.desc { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.meta { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); min-width: 0; }
.meta time { white-space: nowrap; }
.cat { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cat-review { color: var(--warn-text); background: var(--warn-bg); border-radius: 6px; padding: 0 6px; justify-self: start; }
.amount { flex: none; font-weight: 700; font-variant-numeric: tabular-nums; }
.amount.credit, strong.credit { color: var(--credit); }

.toast {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-bottom: 16px; padding: 10px 14px; border-radius: var(--radius);
  background: var(--accent-soft); border: 1px solid var(--accent);
}
.toast form { margin: 0; }

/* ── Edit / refund / review ───────────────────────────────────────── */
.refunds, .original { margin-top: 16px; }
.original { margin: 0 0 16px; }
.original p { margin: 4px 0 0; }
.refunds ul { list-style: none; margin: 8px 0 0; padding: 0; }
.refunds li { display: flex; justify-content: space-between; gap: 12px; padding: 6px 0; }
.review { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.review-head { display: flex; justify-content: space-between; gap: 12px; }
.review p { margin: 2px 0 10px; }
.review .chips { margin-bottom: 10px; }
.review-pick { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.whoami-options { display: grid; gap: 12px; margin-top: 20px; }

/* ── Dashboard ────────────────────────────────────────────────────── */
.month-title { font-size: 20px; }
.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-bottom: 16px; }
@media (min-width: 760px) { .kpis { grid-template-columns: repeat(4, 1fr); } }
.tile {
  display: grid; gap: 2px; align-content: start; padding: 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.tile-value { font-size: 22px; font-weight: 650; }
.delta { font-size: 12px; color: var(--muted); }
.delta-up { color: var(--status-up); font-weight: 600; }
.delta-down { color: var(--status-down); font-weight: 600; }

.dash-grid { display: grid; gap: 16px; }
@media (min-width: 960px) { .dash-grid { grid-template-columns: 3fr 2fr; align-items: start; } }
.chart { display: grid; gap: 14px; }
.chart-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 8px 16px; }
.chart-head h2 { font-size: 16px; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; list-style: none; margin: 0; padding: 0; font-size: 13px; color: var(--muted); }
.legend li { display: flex; align-items: center; gap: 6px; }
.legend-value { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.swatch { width: 10px; height: 10px; border-radius: 2px; }
.series-1 { background: var(--series-1); }
.series-2 { background: var(--series-2); }

.hbars { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.hbar {
  display: grid; grid-template-columns: 1fr auto; grid-template-areas: "label value" "track track";
  gap: 4px 12px; padding: 6px 8px; margin: 0 -8px; border-radius: 8px; cursor: default;
}
.hbar:hover, .hbar:focus-visible { background: var(--bg); }
.hbar:focus-visible { outline: 2px solid var(--accent); outline-offset: 0; }
@media (min-width: 600px) {
  .hbar { grid-template-columns: 150px 1fr 90px; grid-template-areas: "label track value"; align-items: center; }
  .hbar-value { text-align: right; }
}
.hbar-label { grid-area: label; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hbar-value { grid-area: value; font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.hbar-track { grid-area: track; display: flex; gap: 2px; height: 14px; }
.seg { height: 100%; min-width: 2px; }
.seg:last-child { border-radius: 0 4px 4px 0; }

.table-view summary { cursor: pointer; color: var(--accent); font-weight: 600; font-size: 14px; }
.table-scroll { overflow-x: auto; margin-top: 10px; }
.table-view table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table-view th, .table-view td { padding: 6px 8px; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; }
.table-view th[scope="row"], .table-view thead th:first-child { text-align: left; font-weight: 500; }
.table-view td { font-variant-numeric: tabular-nums; }
.table-view tfoot th, .table-view tfoot td { font-weight: 700; border-bottom: 0; }

.cols-wrap { position: relative; padding-left: 40px; }
.cols-grid { position: absolute; left: 0; right: 0; top: 0; height: 180px; pointer-events: none; }
.gridline { position: absolute; left: 40px; right: 0; border-top: 1px solid var(--grid); }
.gridline span { position: absolute; right: calc(100% + 6px); top: -8px; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.cols { list-style: none; margin: 0; padding: 0; display: flex; gap: 2px; }
.cols li { flex: 1; min-width: 0; }
.col { display: grid; color: var(--muted); border-radius: 6px; }
.col-plot { position: relative; height: 180px; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; }
.col-bar { width: 100%; max-width: 24px; background: var(--context); border-radius: 4px 4px 0 0; min-height: 1px; }
.col-value { font-size: 11px; font-weight: 700; color: var(--text); margin-bottom: 4px; white-space: nowrap; }
.col-label { text-align: center; font-size: 11px; padding-top: 6px; border-top: 1px solid var(--border); }
.col.current .col-bar { background: var(--series-1); }
.col.current .col-label { color: var(--text); font-weight: 700; }
.col:hover .col-bar { filter: brightness(0.92); }

.export { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 20px; }
.export .label { grid-column: 1 / -1; }
@media (min-width: 760px) { .export { grid-template-columns: auto auto auto; justify-content: start; align-items: center; } .export .label { grid-column: auto; } }

.tooltip {
  position: absolute; z-index: 20; min-width: 160px; max-width: 260px; padding: 8px 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 4px 16px rgb(16 24 40 / 14%); font-size: 13px; pointer-events: none;
}
.tip-title { font-weight: 700; margin-bottom: 4px; }
.tip-row { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.tip-row strong { color: var(--text); font-variant-numeric: tabular-nums; }
.tip-key { width: 12px; height: 3px; border-radius: 2px; flex: none; }

/* ── Description autofill ─────────────────────────────────────────── */
.combo { position: relative; }
.suggestions {
  position: absolute; z-index: 15; top: calc(100% + 4px); left: 0; right: 0;
  list-style: none; margin: 0; padding: 4px; max-height: 280px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 8px 24px rgb(16 24 40 / 14%);
}
.suggestion { display: grid; gap: 1px; padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.suggestion:hover, .suggestion[aria-selected="true"] { background: var(--accent-soft); }
.suggestion-text { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.suggestion-meta { font-size: 12px; color: var(--muted); }

/* ── Sign in / account ────────────────────────────────────────────── */
.auth { max-width: 420px; padding-top: 24px; }
.auth-form { display: grid; gap: 14px; margin-top: 12px; }
input[type="password"] {
  width: 100%; min-height: 46px; padding: 10px 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
}
.hint { margin-top: 16px; font-size: 13px; }
.hint code { font-size: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 2px 6px; }

/* ── Settings ─────────────────────────────────────────────────────── */
.settings-link { display: flex; align-items: center; justify-content: space-between; gap: 12px; color: var(--text); margin: 16px 0; font-size: 15px; }
.settings-link > span:last-child { font-size: 22px; color: var(--muted); }
.cat-list { list-style: none; margin: 16px 0; padding: 0; display: grid; gap: 10px; }
.cat-item { display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: start; padding: 10px 12px; }
.cat-hidden { opacity: .6; }
.cat-item.has-error { border-color: var(--danger); }
.cat-item summary { display: grid; gap: 1px; cursor: pointer; list-style: none; padding: 4px 0; min-height: 40px; }
.cat-item summary::-webkit-details-marker { display: none; }
.cat-name { font-weight: 600; }
.cat-name::after { content: ' ›'; color: var(--muted); }
details[open] .cat-name::after { content: ' ⌄'; }
.cat-summary { font-size: 13px; }
.cat-form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; align-items: end; margin-top: 10px; }
.cat-form .check { align-self: center; }
@media (max-width: 480px) { .cat-form { grid-template-columns: 1fr; } }
.cat-move { display: flex; gap: 6px; }
.cat-move form { margin: 0; }
.icon-btn:disabled { opacity: .35; cursor: default; }
button.icon-btn { cursor: pointer; font: inherit; font-size: 18px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.cat-new { display: grid; gap: 12px; margin-top: 8px; }
