:root {
  color-scheme: light;
  --bg: #f6f6f3;
  --surface: #ffffff;
  --surface-2: #f0f0ec;
  --text: #141413;
  --muted: #66655f;
  --line: #e4e3dd;
  --grid: #ebeae5;
  --series: #2a78d6;
  --series-wash: rgba(42, 120, 214, 0.1);
  --pos: #1a7a3a;
  --neg: #c0362f;
  --on-neg: #ffffff;
  --amber: #8a5600;
  --amber-bg: #fdf0d2;
  --focus: #2a78d6;
  --radius: 14px;
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #242422;
    --text: #f0efec;
    --muted: #a6a59d;
    --line: #2f2f2c;
    --grid: #2a2a27;
    --series: #3987e5;
    --series-wash: rgba(57, 135, 229, 0.12);
    --pos: #57c278;
    --neg: #f07a72;
    --on-neg: #1a0d0c;
    --amber: #f0b54d;
    --amber-bg: #3a2c10;
    --focus: #5a9ff0;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--font);
  overflow-x: hidden;
}
a { color: inherit; }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
p { margin: 0; }
small, .small { font-size: 13px; }
.muted { color: var(--muted); }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.error { color: var(--neg); }
.ok { color: var(--pos); }
.amber-text { color: var(--amber); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 6px; }

.wrap { max-width: 760px; margin: 0 auto; padding: 0 16px 64px; }

/* ---------- header ---------- */

.top {
  position: sticky; top: 0; z-index: 10;
  padding-top: env(safe-area-inset-top);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
}
.top-in { max-width: 760px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; gap: 4px; }
.brand { font-weight: 700; text-decoration: none; margin-right: auto; letter-spacing: -0.01em; white-space: nowrap; }
.top nav { display: flex; gap: 2px; }
.top nav a, .top .link {
  text-decoration: none; padding: 6px 10px; border-radius: 8px; color: var(--muted); font-size: 15px;
}
.top nav a[aria-current="page"] { color: var(--text); background: var(--surface-2); }

/* ---------- hero ---------- */

.hero { padding: 24px 0 18px; }
.hero .label { color: var(--muted); font-size: 14px; }
.big {
  font-size: clamp(40px, 12vw, 60px); line-height: 1.05; font-weight: 650;
  letter-spacing: -0.025em; margin: 4px 0 6px; overflow-wrap: anywhere;
}
.big.medium { font-size: clamp(32px, 9vw, 44px); }
.hero .sub { color: var(--muted); font-size: 14px; }
.hero .line { margin-top: 10px; }
.hero .line + .line { margin-top: 2px; }
.hero-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }

/* ---------- cards & folds ---------- */

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin-top: 12px;
}
.card > h2 + * { margin-top: 10px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
details > summary { cursor: pointer; list-style: none; }
details > summary::-webkit-details-marker { display: none; }
details.fold > summary { display: flex; align-items: center; gap: 8px; font-weight: 600; }
details.fold > summary::after, .group > summary .name::before {
  content: ""; width: 7px; height: 7px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted);
  transform: rotate(-45deg); transition: transform 0.15s; flex: none;
}
details.fold > summary::after { margin-left: auto; }
details.fold[open] > summary::after, .group[open] > summary .name::before { transform: rotate(45deg); }
details.fold[open] > summary { margin-bottom: 10px; }
.empty { text-align: left; }
.empty p { margin-top: 6px; color: var(--muted); }
.empty .actions { margin-top: 12px; }

/* ---------- groups on home ---------- */

.groups { margin-top: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.group + .group { border-top: 1px solid var(--line); }
.group > summary, .rows a {
  display: grid; grid-template-columns: minmax(0, 1fr) auto 3.4em; gap: 10px; align-items: center;
  padding: 13px 16px;
}
.group > summary .name { display: flex; align-items: center; gap: 10px; font-weight: 600; min-width: 0; }
.group .val { font-variant-numeric: tabular-nums; white-space: nowrap; text-align: right; }
.group > summary .val { font-weight: 600; }
.group .share { color: var(--muted); font-size: 14px; text-align: right; font-variant-numeric: tabular-nums; }
.rows { list-style: none; margin: 0; padding: 0 0 6px; }
.rows a { text-decoration: none; padding-top: 8px; padding-bottom: 8px; padding-left: 33px; }
.rows a:hover { background: var(--surface-2); }
.rows .acc { min-width: 0; display: flex; flex-direction: column; }
.rows .acc .n { display: flex; gap: 8px; align-items: center; min-width: 0; }
.rows .acc .n span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rows .acc small { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge {
  display: inline-block; font-size: 12px; line-height: 1; padding: 4px 7px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); white-space: nowrap; font-weight: 500;
}
.badge.amber { background: var(--amber-bg); color: var(--amber); }

/* ---------- chart ---------- */

.chart-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.chart-sum { margin-top: 10px; font-size: 14px; color: var(--muted); }
.chart { position: relative; margin-top: 8px; min-height: 200px; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .tick { fill: var(--muted); font-size: 11.5px; font-variant-numeric: tabular-nums; }
.chart .line { fill: none; stroke: var(--series); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--series-wash); stroke: none; }
.chart .dot { fill: var(--series); stroke: var(--surface); stroke-width: 2; }
.chart .cross { stroke: var(--muted); stroke-width: 1; opacity: 0.6; }
.chart .hit { fill: transparent; cursor: crosshair; }
.chart-tip {
  position: absolute; top: -6px; transform: translateY(-100%); pointer-events: none;
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 5px 9px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.08); white-space: nowrap; font-size: 13px;
  display: flex; flex-direction: column; line-height: 1.3;
}
.chart-tip span { color: var(--muted); font-size: 12px; }
.chart-empty { padding: 70px 0; text-align: center; }
.chart.loading { opacity: 0.5; }

/* ---------- controls ---------- */

button, .btn {
  font: inherit; font-size: 15px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); padding: 8px 14px; cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; gap: 6px; min-height: 38px;
}
button:hover { background: var(--surface-2); }
button:disabled { opacity: 0.55; cursor: default; }
button.primary { background: var(--text); color: var(--bg); border-color: var(--text); font-weight: 600; }
button.primary:hover { opacity: 0.9; }
button.link, .link {
  border: 0; background: none; padding: 4px 6px; min-height: 0; color: var(--muted); text-decoration: underline;
  text-underline-offset: 3px; font-size: 14px;
}
button.link.danger { color: var(--neg); }
button.small { font-size: 14px; padding: 5px 10px; min-height: 32px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: block; padding: 5px 11px; border-radius: 8px; font-size: 14px; cursor: pointer; color: var(--muted);
  user-select: none;
}
.seg input:checked + span { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12); font-weight: 600; }
.seg input:focus-visible + span { outline: 2px solid var(--focus); }

.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--series); }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chips button { font-size: 14px; min-height: 34px; padding: 5px 11px; border-radius: 999px; }
.chips button[aria-pressed="true"], .chips a[aria-current] { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip {
  font-size: 14px; min-height: 34px; padding: 5px 12px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); text-decoration: none; display: inline-flex; align-items: center;
}

/* ---------- forms ---------- */

form.form { margin-top: 14px; display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 560px) { form.form.two { grid-template-columns: 1fr 1fr; } form.form .wide { grid-column: 1 / -1; } }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field .label { font-size: 13px; color: var(--muted); font-weight: 500; }
.field .hint { font-size: 12.5px; color: var(--muted); }
.field .err { font-size: 12.5px; color: var(--neg); min-height: 0; }
.field .err:empty { display: none; }
input, select, textarea {
  font: inherit; font-size: 16px; /* 16px keeps iOS from zooming */
  color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 11px; width: 100%; min-width: 0;
}
input[type="checkbox"], input[type="radio"] { width: auto; }
input[aria-invalid] { border-color: var(--neg); }
textarea { resize: vertical; min-height: 60px; }
.check { display: flex; gap: 8px; align-items: center; font-size: 15px; }
.form-msg { font-size: 14px; }
.form-msg:empty { display: none; }
.form-foot { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* ---------- tables & lists ---------- */

.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
table { border-collapse: collapse; width: 100%; font-size: 15px; }
th { font-size: 12.5px; font-weight: 500; color: var(--muted); text-align: left; padding: 6px 8px 6px 0; border-bottom: 1px solid var(--line); }
td { padding: 9px 8px 9px 0; border-bottom: 1px solid var(--line); vertical-align: top; }
th:last-child, td:last-child { padding-right: 0; }
tr:last-child td { border-bottom: 0; }
td small { display: block; color: var(--muted); font-size: 12.5px; }
th.num { text-align: right; }

.list { list-style: none; margin: 0; padding: 0; }
.list > li { padding: 10px 0; border-bottom: 1px solid var(--line); display: flex; gap: 10px; align-items: flex-start; }
.list > li:last-child { border-bottom: 0; }
.list .grow { flex: 1; min-width: 0; }
.list .meta { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.entry.voided .grow { text-decoration: line-through; opacity: 0.55; }
.entry .type { font-weight: 600; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; margin: 0; font-size: 15px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }

.review li { padding: 6px 0; font-size: 14px; }
.back { display: inline-block; margin-top: 16px; color: var(--muted); text-decoration: none; font-size: 14px; }

.item > summary { display: flex; gap: 10px; align-items: baseline; padding: 10px 0; }
.item > summary .grow { flex: 1; min-width: 0; }
.item > summary .grow small { display: block; }
.item + .item { border-top: 1px solid var(--line); }
.item[open] { padding-bottom: 14px; }

/* scenario change rows */
.change { border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: grid; gap: 10px; }
.change-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; align-items: end; }
.change-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.changes { display: grid; gap: 10px; }
.result-groups { list-style: none; margin: 10px 0 0; padding: 0; }
.result-groups li { display: flex; justify-content: space-between; gap: 12px; padding: 5px 0; border-bottom: 1px solid var(--line); }
.result-groups li:last-child { border-bottom: 0; }

/* ---------- sign-in, toast ---------- */

.center { text-align: center; padding-top: 18vh; }
.center p { margin: 10px auto 22px; max-width: 34ch; }
.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; max-width: calc(100vw - 32px); z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--neg); color: var(--on-neg); }
.loading-line { padding: 40px 0; color: var(--muted); }

@media (max-width: 420px) {
  .top nav a, .top .link { padding: 6px 7px; font-size: 14px; }
  .group > summary, .rows a { grid-template-columns: minmax(0, 1fr) auto 2.9em; gap: 8px; padding-left: 12px; padding-right: 12px; }
  .rows a { padding-left: 29px; }
  .card { padding: 14px; }
  .table-wrap { margin: 0 -14px; padding: 0 14px; }
}

/* Narrowest phones (320 px): the brand gives way so the nav and Sign out fit; Home is in the nav. */
@media (max-width: 359px) {
  .brand { display: none; }
  .top nav { margin-right: auto; }
  .top nav a, .top .link { padding: 6px 6px; }
}
