:root {
  --bg: #f4f4f2;
  --card: #ffffff;
  --ink: #17181a;
  --muted: #6d7075;
  --line: #e2e2df;
  --accent: #2f6f4f;
  --accent-ink: #ffffff;
  --pos: #1f7a4d;
  --neg: #b4452f;
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171a;
    --card: #1f2125;
    --ink: #e9e9e7;
    --muted: #9a9da3;
    --line: #2e3136;
    --accent: #4a9c72;
    --accent-ink: #0d1210;
    --pos: #5fbf8c;
    --neg: #e2795f;
  }
}

* { box-sizing: border-box; }

/* Авторское `label { display: block }` перебивает браузерное [hidden] — возвращаем приоритет. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 12px 48px;
}
main.narrow { max-width: 380px; padding-top: 48px; }

h1 { font-size: 1.3rem; margin: 0 0 16px; }
h2 { font-size: 1rem; margin: 0 0 10px; letter-spacing: .01em; }

a { color: var(--accent); }

/* --- шапка --- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  max-width: 720px;
  margin: 0 auto;
  padding: 12px;
}
.brand { font-weight: 650; text-decoration: none; color: var(--ink); }
.topbar nav { display: flex; align-items: center; gap: 12px; font-size: .875rem; }
.topbar form { margin: 0; }

/* --- карточки --- */
.card, .summary, .history {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}

/* --- сводка --- */
/* Не закреплена: при прокрутке длинной истории приклеенный сверху блок мешает.
   Раз он уезжает вместе со страницей, ограничивать его высоту и заводить
   внутреннюю прокрутку тоже не нужно — ничего не обрежется. */
.summary {
  margin-bottom: 14px;
}

/* Долги в несколько колонок: на телефоне одна, дальше столько, сколько влезает. */
.transfers {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  column-gap: 20px;
}
.transfers li {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 8px;
  padding: 5px 0;
  border-bottom: 1px dashed var(--line);
}
.transfers .settled { grid-column: 1 / -1; border-bottom: 0; justify-content: center; color: var(--muted); padding: 8px 0; }

/* Группа должника: сверху общий долг, под ним — план платежей. */
.transfers .debtor { display: block; padding: 8px 0; }
.debtor-head { display: flex; align-items: baseline; gap: 6px; }
.debtor-head .who { font-weight: 650; }
.debtor-head .owes { font-size: .75rem; color: var(--muted); }
.debtor-head .amt { margin-left: auto; font-weight: 650; font-variant-numeric: tabular-nums; }
.transfers .to { list-style: none; margin: 4px 0 0; padding: 0 0 0 10px; }
.transfers .to li {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 8px;
  padding: 3px 0;
  border-bottom: 0;
}
.transfers .to .who { font-weight: 400; }
.transfers .to .amt { margin-left: auto; font-variant-numeric: tabular-nums; white-space: nowrap; }

.hint.plan { margin-top: 10px; padding-top: 8px; border-top: 1px dashed var(--line); }

.flash {
  margin: 0 0 12px;
  padding: 10px 12px;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: var(--card);
  font-size: .875rem;
}
.transfers .who { font-weight: 600; }
.transfers .arrow { color: var(--muted); }
.transfers .amt { margin-left: auto; font-variant-numeric: tabular-nums; white-space: nowrap; }
.transfers .settled { justify-content: center; color: var(--muted); padding: 8px 0; }
.transfers form { margin: 0; }
.settle {
  padding: 3px 10px;
  font-size: .8125rem;
  line-height: 1.3;
  border-radius: 999px;
  border-color: var(--accent);
  color: var(--accent);
  background: transparent;
}
.settle:hover { background: var(--accent); color: var(--accent-ink); }

.balances { margin-top: 10px; font-size: .875rem; }
.balances summary { cursor: pointer; color: var(--muted); }
.balances ul {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  column-gap: 20px;
}
.balances li { display: flex; justify-content: space-between; padding: 3px 0; }
.balances .amt { font-variant-numeric: tabular-nums; }
.balances .pos .amt { color: var(--pos); }
.balances .neg .amt { color: var(--neg); }
.balances .zero .amt { color: var(--muted); }

/* --- формы --- */
label { display: block; font-size: .8125rem; color: var(--muted); margin-bottom: 10px; }
label.inline { display: inline-flex; align-items: center; gap: 6px; margin: 0; }

input[type="text"], input[type="password"], input[type="date"], select {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 9px 10px;
  font: inherit;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type="checkbox"] { accent-color: var(--accent); }

.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.row > label { flex: 0 1 auto; }
.row > label.grow { flex: 1 1 120px; }

button {
  font: inherit;
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  color: var(--ink);
  cursor: pointer;
}
button.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
  font-weight: 600;
}
button.linkish {
  border: 0;
  background: none;
  padding: 0;
  color: var(--accent);
  font-size: inherit;
}
button.linkish.danger { color: var(--neg); }

.actions { display: flex; align-items: center; gap: 14px; margin-top: 4px; }
.cancel { font-size: .875rem; }

/* --- переключатель расход / возврат --- */
.tabs { display: flex; gap: 6px; margin-bottom: 12px; }
.tab { flex: 1; padding: 7px; font-size: .875rem; color: var(--muted); }
.tab.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }

/* --- «за кого» --- */
.label-row { display: flex; justify-content: space-between; align-items: baseline; font-size: .8125rem; color: var(--muted); }
.quick button { border: 0; background: none; padding: 0; color: var(--accent); font-size: inherit; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 12px; }
.chip { margin: 0; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span {
  display: block;
  padding: 6px 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
  font-size: .875rem;
  cursor: pointer;
  user-select: none;
}
.chip input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.converted { margin: -4px 0 10px; font-size: .8125rem; color: var(--muted); font-variant-numeric: tabular-nums; }

/* --- история --- */
.txs { list-style: none; margin: 0; padding: 0; }
.txs li { padding: 9px 0; border-bottom: 1px solid var(--line); }
.txs li:last-child { border-bottom: 0; }
.line1 { display: flex; align-items: baseline; gap: 8px; }
.line2 { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-top: 2px; font-size: .8125rem; color: var(--muted); }
.line1 .date { color: var(--muted); font-size: .8125rem; font-variant-numeric: tabular-nums; min-width: 2.8em; }
.line1 .note { font-weight: 550; }
.line1 .sum { margin-left: auto; font-variant-numeric: tabular-nums; white-space: nowrap; }
.line2 .payer { font-weight: 550; color: var(--ink); }
.line2 .tools { margin-left: auto; display: flex; gap: 10px; align-items: baseline; }
.line2 .tools form { margin: 0; }
.line2 .warn { color: var(--neg); }
.txs li.repayment .note { color: var(--pos); }

.trashlink { margin: 12px 0 0; font-size: .8125rem; }
.trashlink a { color: var(--muted); }

/* --- настройки --- */
/* Имя — на всю ширину, «участвует» и кнопка — второй строкой по краям.
   На узком экране это заметно компактнее, чем flex-перенос. */
.people { list-style: none; margin: 0; padding: 0; }
.person-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  padding-bottom: 10px;
  margin-bottom: 10px;
  border-bottom: 1px solid var(--line);
}
.person-row input[type="text"] { grid-column: 1 / -1; margin-top: 0; }
.people li:last-child .person-row { border-bottom: 0; }
.add-person { border-bottom: 0; padding-bottom: 0; margin-bottom: 0; }
.add-person button { grid-column: 2; }

.hint { font-size: .8125rem; color: var(--muted); margin: 8px 0 0; }
.empty { color: var(--muted); }
.error { color: var(--neg); }
.ok { color: var(--pos); }
