:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --text: #1f2933;
  --muted: #64707d;
  --line: #e2ddd3;
  --accent: #0e7490;
  --accent-soft: #e0f2f5;
  --danger: #b42318;
  --hint: #fff7e0;
  --hint-line: #f0d68a;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181c;
    --surface: #1d2328;
    --text: #e6e9ec;
    --muted: #9aa5b1;
    --line: #313a42;
    --accent: #4fc3dc;
    --accent-soft: #173a44;
    --danger: #f97066;
    --hint: #2e2a1a;
    --hint-line: #6b5a1e;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

header {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: 12px 16px 0;
  position: sticky;
  top: 0;
  z-index: 3;
}

h1 { margin: 0; font-size: 1.3rem; }
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.brand img { width: 32px; height: 32px; border-radius: 7px; flex: none; }
h2 { font-size: 1.05rem; margin: 28px 0 8px; }
h3 { margin: 0; font-size: 1rem; }

nav { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
nav a {
  white-space: nowrap;
  padding: 8px 12px;
  color: var(--muted);
  text-decoration: none;
  border-bottom: 3px solid transparent;
}
nav a.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

main { max-width: 1100px; margin: 0 auto; padding: 16px; }

a { color: var(--accent); }
.muted { color: var(--muted); }
.hint { background: var(--hint); border: 1px solid var(--hint-line); border-radius: 8px; padding: 10px 12px; }
.tag { font-size: 0.75rem; background: var(--accent-soft); color: var(--accent); border-radius: 4px; padding: 0 5px; }

/* ---- cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; margin-bottom: 8px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; }
.card span { display: block; color: var(--muted); font-size: 0.85rem; }
.card b { font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.card.strong { border-color: var(--accent); background: var(--accent-soft); }

.group-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }
.group-card p { margin: 8px 0 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > label { flex: 1 1 160px; }

/* ---- forms */
input, select, button { font: inherit; color: inherit; }
input, select {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 8px;
  min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); }
input.amount { width: 7em; text-align: right; }
input.currency { width: 4.5em; text-transform: uppercase; }
input.name { width: 100%; min-width: 9em; }
.row input.name { flex: 1; }

button {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 6px;
  padding: 7px 14px;
  cursor: pointer;
  font-weight: 600;
}
@media (prefers-color-scheme: dark) { button { color: #0b1a1f; } }
button.small, button.icon { background: transparent; color: var(--accent); padding: 4px 8px; font-weight: 500; }
button.icon.danger { color: var(--danger); }

form.add, form.settings {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
}
/* Grid, not a flex column: a column flex sizes each wrapping .row as if its fields were stacked. */
form.settings { display: grid; align-items: stretch; max-width: 560px; }
form.settings button { justify-self: start; }
form.settings p { margin: 0; }
form label { display: flex; flex-direction: column; gap: 3px; font-size: 0.85rem; color: var(--muted); }
form label input, form label select { color: var(--text); font-size: 15px; }

/* ---- tables */
.scroll { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th, td { padding: 6px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
thead th { font-size: 0.8rem; color: var(--muted); font-weight: 600; vertical-align: bottom; }
tbody tr:last-child > * { border-bottom: 0; }
tfoot > tr > * { border-top: 2px solid var(--line); border-bottom: 0; font-weight: 600; }
.num { text-align: right; }
.actions { text-align: right; }
table.narrow { width: auto; min-width: 280px; }

.summary tr.group > * { background: var(--accent-soft); font-weight: 600; }
.summary tr.member th { font-weight: 400; padding-left: 26px; }
.summary .due { font-weight: 600; }

.grid td, .grid th { padding: 4px 6px; }
.grid .night { text-align: center; min-width: 40px; }
.grid .sticky { position: sticky; left: 0; background: var(--surface); z-index: 1; }
.grid tbody .sticky { min-width: 140px; }
.list td input:not(.amount) { width: 100%; min-width: 12em; }

#toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 8px;
  max-width: calc(100% - 32px);
  z-index: 10;
}

@media (max-width: 600px) {
  main { padding: 12px; }
  form.add > label { flex: 1 1 100%; }
  form.add > label:has(.child), form.add > label:has(.amount) { flex: 1 1 40%; }
}

@media print {
  header nav, form, #toast, .actions, button { display: none !important; }
  header { position: static; }
  .scroll { overflow: visible; border: 0; }
}

/* ---- small fixes */
.scroll:has(> table.narrow) { display: inline-block; max-width: 100%; }
@media (max-width: 600px) {
  .grid select { max-width: 8.5em; }
  .grid tbody .sticky { min-width: 110px; }
}
