:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1c2330;
  --muted: #5d6778;
  --border: #dde1e7;
  --primary: #1f5fbf;
  --primary-text: #ffffff;
  --positive: #1d7a45;
  --negative: #b3261e;
  --forecast: #8a5a00;
  --forecast-bg: #fff6e0;
  --actual-bg: #e5f4ea;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --surface: #1b2029;
    --surface-2: #232a35;
    --text: #e6e9ef;
    --muted: #9aa4b5;
    --border: #2e3644;
    --primary: #5b9bff;
    --primary-text: #0b1220;
    --positive: #5cc98a;
    --negative: #ff8a80;
    --forecast: #f0c060;
    --forecast-bg: #2e2716;
    --actual-bg: #173022;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  font-variant-numeric: tabular-nums;
}
a { color: var(--primary); }
h1 { font-size: 1.5rem; margin: 0 0 0.75rem; }
h2 { font-size: 1.1rem; margin: 0 0 0.75rem; }
code { background: var(--surface-2); padding: 0.1em 0.35em; border-radius: 4px; font-size: 0.9em; word-break: break-all; }
hr { border: 0; border-top: 1px solid var(--border); margin: 1.25rem 0; }

/* Layout */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 1rem;
  padding: 0.5rem 1rem;
  /* Home screen app on iPhone: keep clear of the status bar/notch */
  padding-top: calc(0.5rem + env(safe-area-inset-top));
  padding-left: calc(1rem + env(safe-area-inset-left));
  padding-right: calc(1rem + env(safe-area-inset-right));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; text-decoration: none; color: var(--text); white-space: nowrap; }
.nav { display: flex; gap: 0.25rem; overflow-x: auto; flex: 1; scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }
.nav a {
  padding: 0.4rem 0.7rem; border-radius: 6px; text-decoration: none; color: var(--muted); white-space: nowrap;
}
.nav a[aria-current="page"] { background: var(--surface-2); color: var(--text); font-weight: 600; }
.logout { margin: 0; }
.container { max-width: 1280px; margin: 0 auto; padding: 1rem 1rem 6rem; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.75rem; margin-bottom: 1rem; }
.page-head h1 { margin: 0; }
.month-head h1 { flex: 1; text-align: center; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.section-head h2 { margin: 0; }
.grid-2 { display: grid; gap: 1rem; grid-template-columns: 1fr; }
@media (min-width: 900px) { .grid-2 { grid-template-columns: 1fr 1fr; } }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1rem; margin-bottom: 1rem;
}
.card-flush { padding: 0; overflow: hidden; }
.card-pad { padding: 1rem 1rem 0; }
.card-flush > .card-pad:last-child { padding-bottom: 1rem; }
.form-card { max-width: 560px; margin: 0 auto 1rem; }
.form-card.wide { max-width: 820px; }
.muted { opacity: 0.75; }

/* Stats */
.cards { display: grid; gap: 1rem; grid-template-columns: 1fr; margin-bottom: 1rem; }
@media (min-width: 700px) { .cards { grid-template-columns: 2fr 1fr 1fr; } }
.cards .card { margin: 0; }
.stat-label { color: var(--muted); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.03em; }
.stat-value { font-size: 1.6rem; font-weight: 700; }
.stat-hero .stat-value { font-size: 2.3rem; color: var(--positive); }
.stat-hero .stat-value.neg { color: var(--negative); }
.stat-sub { color: var(--muted); font-size: 0.85rem; }

/* Chart */
.chart-wrap { position: relative; height: 280px; }
.legend { color: var(--muted); font-size: 0.85rem; margin: 0.5rem 0 0; }
.legend-solid, .legend-dashed { display: inline-block; width: 24px; height: 0; border-top: 3px solid var(--primary); vertical-align: middle; margin: 0 0.25rem 0 0.75rem; }
.legend-dashed { border-top-style: dashed; }

/* Tables */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 0.45rem 0.6rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
thead th { font-size: 0.8rem; color: var(--muted); font-weight: 600; background: var(--surface-2); white-space: nowrap; }
.num { text-align: right; white-space: nowrap; }
.neg { color: var(--negative); }
.nowrap { white-space: nowrap; }
.empty-state { color: var(--muted); text-align: center; padding: 1.5rem; }
tfoot th { background: var(--surface-2); }

.year-table { font-size: 0.85rem; }
.year-table th, .year-table td { padding: 0.35rem 0.5rem; }
.year-table .sticky {
  position: sticky; left: 0; z-index: 1; background: var(--surface);
  min-width: 11rem; max-width: 14rem; white-space: normal; font-weight: 500;
  box-shadow: 1px 0 0 var(--border);
}
.year-table thead .sticky { background: var(--surface-2); }
.year-table thead a { color: inherit; text-decoration: none; text-transform: capitalize; }
.year-table td.forecast { background: var(--forecast-bg); font-style: italic; }
.year-table .total { font-weight: 700; background: var(--surface-2); }
.year-table .row-sub td, .year-table .row-sub th { color: var(--muted); }
.year-table .row-member th { font-weight: 400; padding-left: 1.6rem; }
.year-table .row-member td { color: var(--muted); }
.year-table .row-group th, .year-table .row-group td { font-weight: 700; }
.group-toggle {
  display: inline-flex; align-items: center; gap: 0.35rem; width: 100%;
  background: none; border: 0; padding: 0; font: inherit; font-weight: 700; color: inherit; text-align: left; cursor: pointer;
}
.group-toggle:hover { color: var(--primary); }
.group-toggle .caret {
  flex: none; width: 0; height: 0; border-left: 5px solid transparent; border-right: 5px solid transparent;
  border-top: 6px solid currentColor; transition: transform 0.15s;
}
.group-toggle[aria-expanded="false"] .caret { transform: rotate(-90deg); }
.year-table .row-sum th, .year-table .row-sum td { font-weight: 700; border-top: 2px solid var(--border); }
.year-table .row-balance th, .year-table .row-balance td { font-weight: 800; background: var(--actual-bg); border-top: 2px solid var(--text); }
.year-table .row-balance td.forecast { background: var(--forecast-bg); }

.list-table td.row-actions, .row-actions { white-space: nowrap; text-align: right; }
.row-actions a, .row-actions .link { margin-left: 0.5rem; }
tr.projected td { color: var(--forecast); font-style: italic; background: var(--forecast-bg); }
tr.auto td { color: var(--muted); font-style: italic; }

/* Tags & badges */
.tag {
  display: inline-block; font-size: 0.7rem; font-style: normal; padding: 0.05rem 0.4rem; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); vertical-align: middle;
}
.tag-forecast { background: var(--forecast-bg); color: var(--forecast); border-color: transparent; }
.tag-actual { background: var(--actual-bg); color: var(--positive); border-color: transparent; }
.tag-system { background: var(--surface-2); color: var(--primary); }
.badge { font-weight: 600; padding: 0.25rem 0.7rem; border-radius: 999px; }
.badge-forecast { background: var(--forecast-bg); color: var(--forecast); }
.badge-actual { background: var(--actual-bg); color: var(--positive); }
.status-bar { display: flex; align-items: center; justify-content: center; gap: 0.75rem; margin-bottom: 1rem; flex-wrap: wrap; }
.status-bar form { margin: 0; }

/* Summary */
.summary { display: grid; grid-template-columns: 1fr auto; gap: 0.3rem 1rem; margin: 0; }
.summary dt { color: var(--muted); }
.summary dd { margin: 0; text-align: right; white-space: nowrap; }
.summary .strong { font-weight: 700; color: var(--text); border-top: 1px solid var(--border); padding-top: 0.3rem; }
.summary .big { font-size: 1.25rem; }

/* Forms */
.field { margin-bottom: 0.85rem; }
.field label { display: block; font-size: 0.85rem; font-weight: 600; margin-bottom: 0.25rem; }
input[type="text"], input[type="password"], input[type="number"], input[type="date"], input[type="month"], select {
  width: 100%; padding: 0.6rem 0.7rem; font: inherit; font-size: 16px; /* avoids zoom on iOS */
  color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.field-large input { font-size: 1.5rem; font-weight: 600; }
.has-error input, .has-error select { border-color: var(--negative); }
.error-text { color: var(--negative); font-size: 0.85rem; margin: 0.25rem 0 0; }
.help { color: var(--muted); font-size: 0.85rem; margin: 0.25rem 0 0.75rem; }
.notice { background: var(--forecast-bg); color: var(--text); border-radius: 8px; padding: 0.75rem 1rem; }
.callout { background: var(--surface-2); border-radius: 8px; padding: 0.75rem 0.75rem 0; }
.form-grid { display: grid; gap: 0 1rem; grid-template-columns: 1fr; }
@media (min-width: 600px) { .form-grid { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); } }
.form-grid h2 { margin-top: 0.5rem; }
.form-grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.span-all { grid-column: 1 / -1; }
.sub-mobile, .d-short { display: none; }
.actions { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
.inline, form.inline { display: inline; margin: 0; }
.inline-form { display: flex; gap: 0.75rem; align-items: flex-end; flex-wrap: wrap; }
.inline-form .field { margin: 0; }
.grow { flex: 1; min-width: 200px; }
.filters { display: grid; gap: 0 1rem; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); align-items: end; }
.danger-zone { border-top: 1px solid var(--border); margin-top: 1rem; padding-top: 1rem; }
details summary { cursor: pointer; font-weight: 600; margin: 0.5rem 0; }
.choices { border: 0; padding: 0; margin: 0 0 1rem; }
.choice { display: flex; gap: 0.5rem; align-items: flex-start; margin-bottom: 0.5rem; cursor: pointer; }
.indent { margin-left: 1.6rem; }
ul.plain { list-style: none; padding: 0; margin: 0; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.3rem;
  padding: 0.55rem 1rem; font: inherit; font-weight: 600; font-size: 0.95rem; line-height: 1.2;
  border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-primary:hover { background: var(--primary); filter: brightness(1.08); }
.btn-danger { color: var(--negative); border-color: var(--negative); background: transparent; }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-small { padding: 0.3rem 0.65rem; font-size: 0.85rem; }
.btn-block { width: 100%; }
.link { background: none; border: 0; padding: 0; font: inherit; color: var(--primary); cursor: pointer; text-decoration: underline; }
.link.danger { color: var(--negative); }

.fab {
  position: fixed; z-index: 20;
  right: calc(1rem + env(safe-area-inset-right)); bottom: calc(1rem + env(safe-area-inset-bottom));
  padding: 0.85rem 1.2rem; border-radius: 999px; font-weight: 700;
  background: var(--primary); color: var(--primary-text); text-decoration: none; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}

/* Flash */
.flash { padding: 0.7rem 1rem; border-radius: 8px; margin-bottom: 1rem; background: var(--actual-bg); color: var(--positive); font-weight: 500; }
.flash-error { background: #fdecea; color: var(--negative); }
@media (prefers-color-scheme: dark) { .flash-error { background: #3a1d1b; } }

/* Year picker */
.year-picker { display: flex; gap: 0.25rem; flex-wrap: wrap; }
.year-picker a { padding: 0.3rem 0.7rem; border-radius: 6px; text-decoration: none; color: var(--muted); border: 1px solid var(--border); }
.year-picker a[aria-current="page"] { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }

/* Categories */
.category-list { list-style: none; margin: 0; padding: 0; }
.category-list li {
  display: grid; grid-template-columns: 1fr; gap: 0.4rem; padding: 0.75rem 1rem; border-bottom: 1px solid var(--border);
}
@media (min-width: 800px) { .category-list li { grid-template-columns: minmax(260px, 2fr) 1fr auto; align-items: center; } }
.category-list li.archived { opacity: 0.6; }
.category-list .rename { display: flex; gap: 0.5rem; margin: 0; }
.category-list .meta { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.category-list .meta .help { margin: 0; }
.category-list .row-actions { display: flex; gap: 0.35rem; justify-content: flex-start; flex-wrap: wrap; }
@media (min-width: 800px) { .category-list .row-actions { justify-content: flex-end; } }

/* Auth */
.auth-box { max-width: 380px; margin: calc(12vh + env(safe-area-inset-top)) auto 0; }
.auth-title { text-align: center; }
.qr { display: flex; justify-content: center; margin: 1rem 0; }
.qr svg { background: #fff; padding: 8px; border-radius: 8px; max-width: 100%; height: auto; }
.secret { font-size: 0.85rem; }

@media (max-width: 599px) {
  .container { padding: 0.75rem 0.75rem 6rem; }
  .topbar { flex-wrap: wrap; gap: 0.25rem 0.75rem; }
  .nav { order: 3; flex-basis: 100%; }
  .logout { margin-left: auto; }
  .col-comment, .d-long { display: none; }
  .d-short { display: inline; }
  .sub-mobile { display: block; font-size: 0.8rem; color: var(--muted); }
  .list-table th, .list-table td { padding: 0.45rem 0.4rem; }
  .list-table .tag-forecast { display: block; width: fit-content; margin-top: 0.15rem; }
  .row-actions a, .row-actions .link { display: block; margin: 0.15rem 0 0; }
  .year-table .sticky { min-width: 8.5rem; }
}
