:root {
  color-scheme: light;
  --brand: hsl(227 63% 50%);
  --brand-hover: #2544b6;
  --page: #f4f6fc;
  --surface: #fff;
  --text: #101a33;
  --muted: #52617b;
  --line: #d9e1f1;
  --soft: #eaf0fb;
  --accent: var(--brand);
  --income: #2f52d0;
  --out: #b45309;
  --grid: #dce3f2;
  --error: #ad303c;
  font: 100%/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0f1014; --surface: #171b25; --text: #f1f4ff; --muted: #b0bed7;
    --line: #323c50; --soft: #20283a; --accent: #a5baff; --income: #91acff;
    --out: #ffab66; --grid: #343e53; --error: #ffa9b1;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0f1014; --surface: #171b25; --text: #f1f4ff; --muted: #b0bed7;
  --line: #323c50; --soft: #20283a; --accent: #a5baff; --income: #91acff;
  --out: #ffab66; --grid: #343e53; --error: #ffa9b1;
}
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-width: 260px; color: var(--text); background: var(--page); }
button, input, textarea { font: inherit; }
button, a, label { -webkit-tap-highlight-color: transparent; }
button, .button, .period label { cursor: pointer; }
:focus-visible { outline: 3px solid var(--income); outline-offset: 3px; }
.shell {
  max-width: 64rem; margin: auto;
  padding: max(1rem, env(safe-area-inset-top), var(--tg-content-safe-area-inset-top, 0px))
           max(1rem, env(safe-area-inset-right)) max(2rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
}
.masthead, .brand, .overview-heading, .summary-top, .dialog-heading, .legend, .key { display: flex; align-items: center; }
.masthead, .summary-top, .overview-heading, .dialog-heading { justify-content: space-between; gap: .75rem; }
.masthead { min-height: 3.75rem; padding-bottom: 1rem; border-bottom: 1px solid var(--line); }
.brand { gap: .65rem; font-size: 1.15rem; font-weight: 750; letter-spacing: -.045em; }
.brand-mark { width: 1.85rem; height: 1.85rem; display: grid; place-items: center; background: var(--brand); border-radius: .55rem; }
.brand-mark::after { content: ""; width: 1.05rem; height: .95rem; background: white; clip-path: polygon(43% 0,66% 0,100% 100%,74% 100%,54% 37%,29% 100%,0 100%); }
.button, .icon-button {
  display: inline-flex; align-items: center; justify-content: center; gap: .25rem;
  min-height: 2.75rem; padding: .55rem 1rem; border: 0; border-radius: .8rem;
  font-weight: 650; color: var(--text); text-decoration: none;
}
.button.primary { background: var(--brand); color: white; }
.button.primary:hover { background: var(--brand-hover); }
.button.secondary, .icon-button { background: var(--soft); }
.button:disabled, .icon-button:disabled { opacity: .6; cursor: wait; }
.compact { min-width: 6rem; }
h1, h2, p { margin: 0; }
h1 { font-size: clamp(2rem, 5vw, 2.9rem); line-height: 1.1; letter-spacing: -.06em; }
h2 { font-size: 1.26rem; line-height: 1.25; letter-spacing: -.035em; }
.overview-heading { flex-wrap: wrap; margin: 1.45rem 0 1.05rem; }
.period-controls { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: .55rem .8rem; min-width: 0; max-width: 100%; }
.period { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: .15rem; min-width: 0; margin: 0; padding: .2rem; background: var(--soft); border: 1px solid var(--line); border-radius: .75rem; }
.period label { position: relative; text-align: center; }
.period input, .sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.period span { display: grid; place-items: center; min-height: 2.75rem; padding: .4rem .55rem; border-radius: .5rem; color: var(--muted); font-size: .9rem; font-weight: 600; white-space: nowrap; }
.period input:checked + span { background: var(--brand); color: white; }
.period input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.month-control, .payment-control { display: flex; align-items: center; flex-wrap: wrap; gap: .45rem .7rem; min-width: 0; max-width: 100%; color: var(--muted); font-size: .9rem; font-weight: 600; }
.month-control input, .payment-control select { min-width: 0; max-width: 100%; min-height: 2.75rem; padding: .45rem .65rem; border: 1px solid var(--line); border-radius: .6rem; background: var(--surface); color: var(--text); font: inherit; }
.payment-control select {
  appearance: none; padding-right: 2rem; cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
                    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: right 1rem center, right .7rem center;
  background-size: .3rem .3rem; background-repeat: no-repeat;
}
.summary { padding: clamp(1.1rem, 3.4vw, 2rem); background: linear-gradient(115deg, #16295f, #223e98 60%, var(--brand)); color: white; border-radius: 1.35rem; }
.summary-top { align-items: flex-start; }
.summary-top h2 { font-size: 1.02rem; font-weight: 550; letter-spacing: -.01em; }
.summary-top span { margin-top: .13rem; text-align: right; font-size: .82rem; color: #dbe5ff; }
.balance-line { display: flex; flex-wrap: wrap; align-items: center; gap: .45rem .9rem; margin: .45rem 0 .45rem; }
.balance { min-width: 0; font-size: clamp(2.4rem, 7vw, 4rem); line-height: 1.16; font-weight: 700; letter-spacing: -.06em; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.balance-status { display: inline-flex; align-items: center; min-height: 1.65rem; padding: .18rem .65rem; border-radius: 2rem; color: #23375c; background: #e2e8f4; font-size: .875rem; font-weight: 700; }
.balance-status[data-state="positive"] { color: #173c85; background: #dae9ff; }
.balance-status[data-state="negative"] { color: #861c29; background: #ffd6dc; }
.balance-method { margin-bottom: 1.1rem; color: #dbe5ff; font-size: .875rem; }
.metrics { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)) minmax(0, .7fr); gap: .75rem; padding-top: 1rem; border-top: 1px solid #ffffff36; }
.metric-label { display: block; color: #dbe5ff; font-size: .875rem; }
.metrics strong { display: block; margin-top: .2rem; font-size: clamp(1.05rem,3.8vw,1.65rem); line-height: 1.25; letter-spacing: -.045em; overflow-wrap: anywhere; }
.panels { display: grid; gap: .85rem; margin-top: .85rem; align-items: start; }
.panel { min-width: 0; padding: clamp(1.1rem, 3vw, 1.65rem); background: var(--surface); border: 1px solid var(--line); border-radius: 1.1rem; }
.legend { flex-wrap: wrap; gap: 1.05rem; margin: .55rem 0 .45rem; color: var(--muted); font-size: .875rem; }
.system-totals { margin-top: .55rem; color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.key { gap: .45rem; }
.key::before { content: ""; width: .9rem; height: .22rem; border-radius: 2px; background: var(--income); }
.expense-key::before { background: repeating-linear-gradient(to right, var(--out) 0 5px, transparent 5px 7px); }
canvas { display: block; width: 100%; height: 12.5rem; touch-action: pan-y; }
.day-detail { display: flex; flex-wrap: wrap; gap: .25rem 1rem; padding: .75rem 0 0; border-top: 1px solid var(--line); color: var(--muted); font-size: .875rem; }
.day-detail strong { color: var(--text); }
.expense-list { list-style: none; padding: 0; margin: .55rem 0 0; }
.expense-list:empty { margin: 0; }
.expense-list li { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: .08rem .7rem; padding: .8rem 0; }
.expense-list li + li { border-top: 1px solid var(--line); }
.expense-list li:last-child { padding-bottom: 0; }
.expense-list .description { font-weight: 550; overflow-wrap: anywhere; }
.expense-list strong { justify-self: end; max-width: 13ch; text-align: end; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
.expense-list time { grid-column: 1 / -1; color: var(--muted); font-size: .875rem; }
.footnote, .state, .gate p { color: var(--muted); }
.footnote { margin-top: .85rem; font-size: .84rem; }
.methodology { max-width: 42rem; }
.state { padding: 1.15rem 0 .35rem; }
.state + .button, .gate .button { margin-top: .85rem; }
.gate { max-width: 34rem; margin: 2rem auto; }
.gate p { margin-top: .8rem; }
#notice { margin-bottom: .8rem; color: var(--accent); }
dialog { width: min(94%, 28rem); max-height: 88vh; overflow-y: auto; margin: auto; padding: clamp(1.1rem,4vw,1.75rem); border: 1px solid var(--line); border-radius: 1.2rem; color: var(--text); background: var(--surface); }
dialog::backdrop { background: #0a132bb8; }
.dialog-heading { margin: 0 0 1.15rem; }
.icon-button { width: 2.75rem; padding: 0; font-size: 1.5rem; }
#expense-form label { display: block; margin: .9rem 0 .35rem; font-weight: 600; }
#expense-form input, textarea { width: 100%; border: 1px solid var(--line); border-radius: .7rem; background: var(--page); color: var(--text); padding: .7rem .8rem; }
textarea { resize: vertical; }
.submit { margin-top: 1.15rem; width: 100%; }
.form-state { margin: .65rem 0 0; color: var(--error); }
@media (max-width: 37rem) {
  .overview-heading { align-items: stretch; flex-direction: column; gap: .85rem; }
  .period-controls { display: grid; justify-items: stretch; }
  .period span { padding-inline: .1rem; }
  .month-control, .payment-control { justify-content: space-between; }
}
@media (max-width: 19rem) {
  .period { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (forced-colors: active) {
  .payment-control select { appearance: auto; }
  .period input:checked + span { outline: 2px solid Highlight; }
}
@media (min-width: 48rem) {
  .shell { padding: 1.75rem 2rem 2.5rem; }
  .panels { grid-template-columns: minmax(0,1.4fr) minmax(0,1fr); gap: 1rem; margin-top: 1rem; }
}
