:root {
    --bg: #f6f5f2;
    --panel: #fff;
    --ink: #202326;
    --muted: #63696e;
    --line: #e3e5e4;
    --accent: #263d32;
    --on-accent: #fff;
    --soft: #e9f0eb;
    --positive: #26704d;
    --negative: #b22828;
    color-scheme: light dark;
    font: 16px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #17191b;
        --panel: #24272a;
        --ink: #f3f4f4;
        --muted: #b9bdc1;
        --line: #41464a;
        --accent: #d5e8dc;
        --on-accent: #15291e;
        --soft: #2c3c32;
        --positive: #9dd6b3;
        --negative: #ffaaaa;
    }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--ink); }
.shell { max-width: 1120px; margin: auto; padding: 24px; }
.brand, .page-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.brand { min-height: 48px; margin-bottom: 22px; font-weight: 500; letter-spacing: .04em; }
.brand-app { color: var(--muted); font-weight: 400; letter-spacing: 0; }
.page-header { margin-bottom: 16px; }
h1, h2, h3, p { margin: 0; }
h1 { margin-bottom: 6px; font-size: 25px; font-weight: 500; line-height: 1.2; letter-spacing: -.03em; }
h2 { margin-bottom: 16px; font-size: 18px; font-weight: 500; }
h3 { color: var(--muted); font-size: 14px; font-weight: 500; }
.page-header p { color: var(--muted); font-size: 14px; }

.button {
    min-height: 48px;
    padding: 10px 14px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--panel);
    color: var(--ink);
    font: inherit;
    font-weight: 500;
    cursor: pointer;
}
.button:hover { background: var(--soft); }
.button:disabled { cursor: default; opacity: .48; }
.button-link { border: 0; background: transparent; color: var(--muted); }
.button-primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.button-primary:hover { background: var(--accent); filter: brightness(1.12); }
.button-small { min-height: 44px; padding: 6px 12px; font-size: 13px; }
:is(button, input, select, textarea, summary):focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }

.overview { margin-bottom: 20px; }
.month-picker { display: grid; grid-template-columns: 48px 1fr 48px; align-items: center; gap: 10px; margin-bottom: 12px; }
.month-picker h2 { margin: 0; text-align: center; }
.month-picker h2::first-letter { text-transform: uppercase; }
.month-arrow { padding: 0; font-size: 28px; line-height: 1; }
.totals { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.total-card, .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 17px; }
.total-card { padding: 16px; min-width: 0; }
.total-card > strong {
    display: block;
    margin-top: 5px;
    font-size: clamp(20px, 3vw, 28px);
    font-weight: 500;
    letter-spacing: -.03em;
    font-variant-numeric: tabular-nums;
    overflow-wrap: anywhere;
}
.total-card.is-negative { border-color: var(--negative); }
.total-card.is-negative .difference { color: var(--negative); }
.month-spent { display: block; margin-top: 8px; color: var(--muted); font-size: 13px; }
.month-spent b { font-weight: 600; overflow-wrap: anywhere; }
.combined { background: var(--soft); }

.layout { display: grid; grid-template-columns: minmax(0, 1fr) 245px; grid-template-areas: "entry calc" "history calc"; gap: 18px 20px; align-items: start; }
.entry { grid-area: entry; scroll-margin-top: 14px; }
.history { grid-area: history; }
.calculator { grid-area: calc; position: sticky; top: 12px; min-width: 0; }
.panel { padding: 20px; min-width: 0; }
.auth { max-width: 420px; margin: min(10vh, 75px) auto 0; }
.auth h1 { margin-bottom: 20px; }
fieldset { min-width: 0; margin: 0; padding: 0; border: 0; }
label { display: block; min-width: 0; font-size: 14px; font-weight: 500; }
input, select, textarea {
    width: 100%;
    min-height: 50px;
    margin-top: 7px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--panel);
    color: var(--ink);
    font: inherit;
    font-size: 16px;
}
input::placeholder, textarea::placeholder { color: var(--muted); }
textarea { display: block; resize: vertical; }
form label + label, .money-fields + label, .conversion + label { margin-top: 12px; }
#entry-value-fields + label { margin-top: 12px; }
.money-fields { display: grid; grid-template-columns: minmax(0, 1fr) 108px; gap: 10px; }
.money-fields label + label, .filters label + label { margin-top: 0; }
.money-fields input, .money-fields select { font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums; }
.conversion { margin-top: 8px; color: var(--muted); font-size: 14px; }
.status { margin-bottom: 14px; padding: 9px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); }
form .status { margin: 12px 0 0; }
.error { color: var(--negative); border-color: var(--negative); }
.success { color: var(--positive); border-color: var(--positive); }
.entry-actions { display: flex; gap: 10px; margin-top: 15px; }
.entry-actions button { flex: 1; }
#auth-submit { width: 100%; margin-top: 15px; }
.change-summary { margin-bottom: 14px; color: var(--muted); white-space: pre-wrap; overflow-wrap: anywhere; }

.calculator summary { min-height: 26px; font-size: 17px; font-weight: 500; cursor: pointer; }
.calculator output {
    display: block;
    padding: 12px;
    border-radius: 12px;
    background: var(--soft);
    font-size: 28px;
    text-align: right;
    font-variant-numeric: tabular-nums;
    overflow-wrap: anywhere;
}
.calc-expression { min-height: 27px; padding: 6px; font-size: 12px; text-align: right; }
.calc-keys { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin: 14px 0; }
.calc-keys button { padding: 4px; background: var(--bg); font-size: 18px; }
.calc-keys button:hover { background: var(--soft); }
.calculator small { color: var(--muted); }
#calc-use { width: 100%; margin-top: 14px; }
#calc-error { margin: 9px 0 0; font-size: 14px; }

.filters { display: flex; flex-wrap: wrap; align-items: end; gap: 10px; margin-bottom: 15px; }
.filter-query { flex: 1; min-width: 150px; }
.extra-filters { flex-basis: 100%; }
.extra-filters summary, .row-details summary { padding: 8px 0; color: var(--muted); cursor: pointer; }
.extra-filters > div, .row-actions { display: flex; flex-wrap: wrap; gap: 8px; }
table { width: 100%; table-layout: fixed; border-collapse: collapse; font-size: 14px; }
th, td { padding: 12px 8px; border-bottom: 1px solid var(--line); vertical-align: top; text-align: left; }
th { color: var(--muted); font-size: 12px; font-weight: 500; }
th:first-child { width: 21%; }
th:last-child { width: 30%; }
td small { display: block; margin-top: 4px; color: var(--muted); font-size: 12px; }
.row-note { white-space: pre-wrap; overflow-wrap: anywhere; }
.row-details { margin-top: 4px; white-space: normal; }
.row-details summary { min-height: 40px; }
.row-details p { margin-bottom: 8px; color: var(--muted); }
.row-sum { color: var(--negative); text-align: right; font-weight: 500; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.inactive .row-sum { color: var(--muted); text-decoration: line-through; }
th.row-sum { color: var(--muted); }
.empty { padding: 24px 0; color: var(--muted); text-align: center; }
.pagination { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 14px; }
.pagination span { color: var(--muted); font-size: 13px; }
#history-toggle { margin-top: 14px; }
dialog { width: min(640px, calc(100% - 30px)); max-height: calc(100% - 30px); overflow: auto; color: var(--ink); }
dialog::backdrop { background: #17191b99; }
.audit-heading { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.audit-heading h2 { margin-bottom: 0; }
.audit-event { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.audit-event small { display: block; color: var(--muted); }
.audit-event p { margin: 7px 0; overflow-wrap: anywhere; white-space: pre-wrap; }
.audit-comparison { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }

@media (max-width: 1000px) {
    .shell { padding: 20px; }
    .layout { grid-template-columns: minmax(0, 1fr) 220px; gap: 14px; }
}
@media (max-width: 760px) {
    .brand { margin-bottom: 16px; }
    .totals { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
    .combined { grid-column: 1 / -1; display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
    .combined > strong { margin-top: 0; text-align: right; }
    .layout { grid-template-columns: 1fr; grid-template-areas: "entry" "calc" "history"; }
    .calculator { position: static; }
    .panel, .total-card { padding: 14px; }
    table, tbody { display: block; }
    thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
    .expense-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, auto); gap: 0 8px; padding: 10px 0; border-bottom: 1px solid var(--line); }
    .expense-row td { border: 0; padding: 3px 0; }
    .expense-row td:nth-child(2) { grid-column: 1 / -1; grid-row: 2; }
    .expense-row td:nth-child(3) { grid-column: 2; grid-row: 1; }
    .audit-comparison { grid-template-columns: 1fr; gap: 4px; }
}
@media (max-width: 360px) {
    .shell { padding: 12px; }
    .panel, .total-card { padding: 12px; }
}
