/* ------------------------------------------------------------------ tokens */

:root {
    color-scheme: light;
    --page: #f4f4f1;
    --surface: #fcfcfb;
    --surface-2: #f0efec;
    --surface-3: #e8e7e2;
    --text: #0b0b0b;
    --text-2: #52514e;
    --muted: #898781;
    --grid: #e1e0d9;
    --axis: #c3c2b7;
    --border: rgba(11, 11, 11, 0.09);
    --shadow: 0 1px 2px rgba(11, 11, 11, 0.05), 0 4px 16px rgba(11, 11, 11, 0.04);
    --accent: #2a78d6;
    --accent-soft: rgba(42, 120, 214, 0.12);
    --series-1: #2a78d6; /* income */
    --series-2: #eb6834; /* spending */
    --series-3: #1baf7a; /* invested */
    --good: #006300;
    --bad: #d03b3b;
    --warn-bg: #fff4d6;
    --radius: 12px;
    --radius-sm: 8px;
    --gap: 18px;
    --control-h: 34px;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;
        --page: #0d0d0d;
        --surface: #1a1a19;
        --surface-2: #232321;
        --surface-3: #2c2c2a;
        --text: #f0efec;
        --text-2: #c3c2b7;
        --muted: #898781;
        --grid: #2c2c2a;
        --axis: #3d3d3a;
        --border: rgba(255, 255, 255, 0.08);
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
        --accent: #3987e5;
        --accent-soft: rgba(57, 135, 229, 0.16);
        --series-1: #3987e5;
        --series-2: #d95926;
        --series-3: #199e70;
        --good: #0ca30c;
        --bad: #e66767;
        --warn-bg: #3a2f10;
    }
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --surface-3: #2c2c2a;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #3d3d3a;
    --border: rgba(255, 255, 255, 0.08);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    --accent: #3987e5;
    --accent-soft: rgba(57, 135, 229, 0.16);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --good: #0ca30c;
    --bad: #e66767;
    --warn-bg: #3a2f10;
}

/* ------------------------------------------------------------------ base */

* { box-sizing: border-box; }

html, body {
    margin: 0;
    background: var(--page);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    font-size: 15px;
    line-height: 1.45;
}
/* Big monitors (1440p and up): a touch larger so it reads comfortably at arm's length. */
@media (min-width: 2200px) { html, body { font-size: 16px; } }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; font-weight: 700; letter-spacing: -0.01em; margin: 0; }
h2 { font-size: 1.05rem; font-weight: 650; margin: 0 0 14px; }
.sub { color: var(--text-2); margin: 4px 0 0; max-width: 120ch; }

/* ------------------------------------------------------------------ shell */

.topbar {
    position: sticky; top: 0; z-index: 10;
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--border);
}
.topbar-inner {
    display: flex; align-items: center; gap: 24px; flex-wrap: wrap;
    max-width: 2400px; margin: 0 auto; padding: 10px clamp(16px, 2.5vw, 48px);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 750; font-size: 1.1rem; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand .logo {
    width: 26px; height: 26px; border-radius: 7px; display: grid; place-items: center;
    background: var(--accent); color: #fff; font-size: .85rem; font-weight: 800;
}
.topbar nav { display: flex; gap: 2px; flex-wrap: wrap; flex: 1; }
.topbar nav a {
    color: var(--text-2); padding: 7px 12px; border-radius: var(--radius-sm); font-weight: 500;
}
.topbar nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.topbar nav a.active { color: var(--accent); background: var(--accent-soft); font-weight: 650; }
.signout { color: var(--muted); font-size: .9rem; background: none; border: none; padding: 4px 6px; height: auto; cursor: pointer; }
.signout:hover { color: var(--text); }
.topbar .user { color: var(--muted); font-size: .85rem; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.auth-page { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; }
.auth-page .brand { font-size: 1.4rem; }
.auth-message { color: var(--text-2); padding: 24px; text-align: center; }

.person-switch { display: inline-flex; align-items: stretch; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.person-switch button {
    border: none; border-radius: 7px; background: transparent; color: var(--text-2);
    padding: 5px 14px; height: auto; font-weight: 550;
}
.person-switch button:hover:not(.on) { background: var(--surface-3); }
.person-switch button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.person-switch .add {
    display: flex; align-items: center; padding: 0 10px; color: var(--muted); border-radius: 7px; font-weight: 600;
}
.person-switch .add:hover { background: var(--surface-3); color: var(--text); text-decoration: none; }

.banner { background: var(--warn-bg); padding: 8px clamp(16px, 2.5vw, 48px); border-bottom: 1px solid var(--border); }

main { max-width: 2400px; margin: 0 auto; padding: 28px clamp(16px, 2.5vw, 48px) 48px; }

/* Page title row: heading + intro on the left, page controls on the right. */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px 24px; flex-wrap: wrap; margin-bottom: 22px; }
.page-head .actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
main > h1 { margin-bottom: 4px; }
main > h1 + .sub { margin-bottom: 22px; }

details.explain { color: var(--text-2); margin-top: 6px; max-width: 120ch; }
details.explain summary { cursor: pointer; color: var(--muted); font-size: .9rem; }
details.explain p { margin: 6px 0 0; }

/* ------------------------------------------------------------------ surfaces */

.card {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 20px 22px; margin-bottom: var(--gap);
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.card-head h2 { margin: 0; }
.card-head .hint { color: var(--muted); font-size: .85rem; }
.card.flush { padding: 0; overflow: hidden; }
.card.flush > .card-head { padding: 18px 22px 0; }

/* Layout helpers: side by side on wide screens, stacked below. */
.cols { display: grid; gap: var(--gap); grid-template-columns: minmax(0, 1fr); margin-bottom: var(--gap); }
.cols > .card { margin-bottom: 0; }
@media (min-width: 1400px) {
    .cols { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .cols.wide-left { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); }
    .cols.wide-right { grid-template-columns: minmax(0, 2fr) minmax(0, 3fr); }
}
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--gap); }

.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar .spacer { flex: 1; }

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

input, select, textarea, button {
    font: inherit; color: var(--text); background: var(--surface);
    border: 1px solid var(--axis); border-radius: var(--radius-sm); padding: 6px 10px;
}
input:not([type=checkbox]):not([type=file]), select, button { min-height: var(--control-h); }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
input[type=checkbox] { padding: 0; width: 16px; height: 16px; accent-color: var(--accent); vertical-align: middle; }
button { cursor: pointer; background: var(--surface-2); border-color: var(--border); font-weight: 550; }
button:hover:not(:disabled) { background: var(--surface-3); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 88%, #000); }
button.link { background: none; border: none; color: var(--accent); padding: 2px 6px; min-height: 0; font-weight: 500; }
button.link:hover:not(:disabled) { background: var(--accent-soft); }
button.danger { color: var(--bad); }

/* ------------------------------------------------------------------ feedback & text */

.error { color: var(--bad); background: color-mix(in srgb, var(--bad) 8%, var(--surface)); border: 1px solid color-mix(in srgb, var(--bad) 40%, transparent); border-radius: var(--radius-sm); padding: 10px 14px; margin-bottom: 16px; }
.ok { color: var(--good); }
.muted { color: var(--muted); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.neg { color: var(--text); }
.pos { color: var(--good); }

.pill { display: inline-block; padding: 1px 9px; border-radius: 999px; font-size: .78rem; font-weight: 550; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.pill.ai { background: var(--accent-soft); color: var(--accent); }
.ai-note { color: var(--text-2); font-size: .85rem; margin-top: 3px; max-width: 70ch; }

/* ------------------------------------------------------------------ tables */

table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: 8px 12px; border-bottom: 1px solid var(--grid); text-align: left; vertical-align: middle; }
th {
    color: var(--muted); font-weight: 650; font-size: .76rem; text-transform: uppercase; letter-spacing: .04em;
    background: var(--surface); white-space: nowrap;
}
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }
tfoot th, tfoot td { border-top: 1px solid var(--axis); border-bottom: none; color: var(--text); font-size: inherit; text-transform: none; letter-spacing: 0; }
td select, td input:not([type=checkbox]) { min-height: 30px; padding: 4px 8px; }
.table-scroll { overflow-x: auto; }
.card.flush .table-scroll, .card.table-scroll[style*="padding:0"] { border-radius: var(--radius); }
.card.flush th:first-child, .card.flush td:first-child { padding-left: 22px; }
.card.flush th:last-child, .card.flush td:last-child { padding-right: 22px; }
tr.group-row td { background: var(--surface-2); color: var(--text-2); font-weight: 650; font-size: .82rem; text-transform: uppercase; letter-spacing: .04em; padding-top: 6px; padding-bottom: 6px; }
tr.clickable { cursor: pointer; }

/* ------------------------------------------------------------------ tiles */

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: var(--gap); margin-bottom: var(--gap); }
.tile {
    position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 16px 20px 16px 22px; overflow: hidden;
}
.tile::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--tile-color, transparent); }
.tile.income { --tile-color: var(--series-1); }
.tile.spending { --tile-color: var(--series-2); }
.tile.invested { --tile-color: var(--series-3); }
.tile .label { color: var(--muted); font-size: .76rem; font-weight: 650; text-transform: uppercase; letter-spacing: .05em; }
.tile .value { font-size: 1.85rem; font-weight: 700; letter-spacing: -0.02em; margin-top: 4px; white-space: nowrap; }
.tile .value .unit { font-size: .55em; font-weight: 600; color: var(--text-2); margin-left: 3px; letter-spacing: 0; }
.tile .delta { color: var(--text-2); font-size: .85rem; margin-top: 2px; }

/* ------------------------------------------------------------------ charts */

.legend { display: flex; gap: 18px; color: var(--text-2); font-size: .85rem; margin-bottom: 10px; flex-wrap: wrap; }
.legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.chart .label { fill: var(--text-2); font-size: 12px; }
.chart .value { fill: var(--text); font-size: 12px; font-variant-numeric: tabular-nums; }
.chart .hit { fill: transparent; cursor: pointer; }
.chart .hit:hover { fill: var(--surface-2); }
.chart .prev { stroke: var(--text); stroke-width: 2; }

/* ------------------------------------------------------------------ misc */

.dropzone {
    border: 2px dashed var(--axis); border-radius: var(--radius); padding: 36px; text-align: center; color: var(--text-2);
    background: var(--surface-2);
}

.empty { text-align: center; padding: 36px 20px; color: var(--text-2); }
.empty h2 { margin-bottom: 6px; }

@media (max-width: 900px) {
    .grid-2 { grid-template-columns: 1fr; }
    main { padding: 16px; }
    .topbar-inner { gap: 10px; }
    .tile .value { font-size: 1.5rem; }
}

/* ------------------------------------------------------------------ blazor boot */

.loading-progress { position: absolute; display: block; width: 6rem; height: 6rem; inset: 20vh 0 auto 0; margin: 0 auto; }
.loading-progress circle { fill: none; stroke: var(--grid); stroke-width: .5rem; transform-origin: 50% 50%; transform: rotate(-90deg); }
.loading-progress circle:last-child { stroke: var(--accent); stroke-dasharray: calc(3.141 * var(--blazor-load-percentage, 0%) * 0.8), 500%; transition: stroke-dasharray .05s ease-in-out; }
.loading-progress-text { position: absolute; text-align: center; font-weight: bold; inset: calc(20vh + 3.25rem) 0 auto .2rem; }
.loading-progress-text:after { content: var(--blazor-load-percentage-text, "Loading"); }

#blazor-error-ui {
    color-scheme: light only; background: lightyellow; bottom: 0; box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2);
    box-sizing: border-box; display: none; left: 0; padding: 0.6rem 1.25rem 0.7rem 1.25rem; position: fixed; width: 100%; z-index: 1000;
}
#blazor-error-ui .dismiss { cursor: pointer; position: absolute; right: 0.75rem; top: 0.5rem; }

/* ------------------------------------------------------------------ split transactions (Transactions page) */

.split-list { display: grid; gap: 2px; margin-bottom: 2px; }
.split-list .num { font-variant-numeric: tabular-nums; }
tr.split-editor > td { background: var(--surface-2); }

/* ------------------------------------------------------------------ rules: who decides */

button.link.warn, .warn-text { color: var(--series-2); font-weight: 650; }
tr.selected > td { background: var(--accent-soft); }
tr.rule-detail > td { background: var(--surface-2); padding: 14px 22px 18px; }
tr.rule-detail:hover { background: none; }
table.inner { background: var(--surface); border-radius: var(--radius-sm); overflow: hidden; }
table.inner th { background: var(--surface); }
.via { color: var(--muted); font-size: .8rem; margin-top: 3px; }
.via a { color: var(--muted); text-decoration: underline dotted; }
.via a:hover { color: var(--accent); }

/* Rows reached by a link scroll below the sticky top bar. */
tr[id^="rule-"] { scroll-margin-top: 90px; }
.toolbar-sep { width: 1px; align-self: stretch; background: var(--border); margin: 0 4px; }

/* ------------------------------------------------------------------ dashboard: expanded category */

.cat-tx { padding: 4px 0; }
.cat-tx-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; color: var(--text-2); flex-wrap: wrap; }
.cat-tx-head .spacer { flex: 1; }
table.compact td { padding: 5px 10px; font-size: .92rem; }
.via-cell { font-size: .85rem; white-space: nowrap; }
tr.expanded > td { background: var(--accent-soft); }
tr.expand-row > td { background: var(--surface-2); padding: 10px 22px 14px; }
tr.expand-row:hover { background: none; }
td.cell-click { cursor: pointer; }
td.cell-click:hover { background: var(--surface-3); }
td.cell-click.on { background: var(--accent-soft); color: var(--accent); font-weight: 650; }
.chevron { display: inline-block; width: 1em; color: var(--muted); transition: transform .15s; }
.chevron.open { transform: rotate(90deg); }

/* Editable transaction list inside an expanded dashboard category */
.cat-tx .flash { color: var(--good); font-size: .88rem; }
table.tx-list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
table.tx-list th { background: var(--surface); font-size: .7rem; padding: 7px 12px; }
table.tx-list td { padding: 7px 12px; vertical-align: top; }
table.tx-list td.date { width: 1%; color: var(--text-2); }
table.tx-list td.desc { min-width: 220px; }
table.tx-list td.account { white-space: nowrap; }
table.tx-list td.amount { width: 1%; font-weight: 600; }
table.tx-list td.cat { width: 1%; white-space: nowrap; }
table.tx-list td.actions { width: 1%; white-space: nowrap; text-align: right; }
table.tx-list tr.editing > td { background: var(--accent-soft); }
.inline-select { border-color: transparent; background: transparent; padding-left: 6px; min-height: 30px; max-width: 220px; font-weight: 550; }
.inline-select:hover, .inline-select:focus { border-color: var(--axis); background: var(--surface); }
tr.editor-row > td { background: var(--surface-2); padding: 12px 16px; }
tr.editor-row:hover { background: none; }
.editor { display: grid; gap: 10px; }
.editor-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.editor-label { font-size: .75rem; font-weight: 650; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); min-width: 110px; }
.show-more { margin-top: 8px; }
.cat-tx.compact table.tx-list td.desc { min-width: 140px; }

/* ------------------------------------------------------------------ modal dialog */

.modal-backdrop {
    position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 24px;
    background: rgba(0, 0, 0, .45); backdrop-filter: blur(2px);
}
.modal {
    width: min(760px, 100%); max-height: calc(100vh - 48px); overflow: auto;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: 0 20px 60px rgba(0, 0, 0, .35); padding: 20px 24px; outline: none;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.modal-head h2 { margin: 0; }
.modal-head .close { font-size: 1.5rem; line-height: 1; color: var(--muted); }
.modal-stats { display: flex; gap: 14px; flex-wrap: wrap; color: var(--text-2); margin-bottom: 16px; font-size: .92rem; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 16px; }
.form-grid label { display: grid; gap: 4px; font-size: .8rem; font-weight: 600; color: var(--text-2); }
.form-grid label.check { display: flex; align-items: center; gap: 8px; align-self: end; font-size: .92rem; }
.form-grid input, .form-grid select { font-size: .95rem; font-weight: 400; width: 100%; }
.form-grid label.check input { width: auto; }
.modal-section { margin-top: 16px; padding: 12px 14px; background: var(--surface-2); border-radius: var(--radius-sm); }
.modal-section-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 6px; }
.lost-list { margin: 0; padding-left: 18px; display: grid; gap: 4px; font-size: .9rem; }
.modal-foot { display: flex; align-items: center; gap: 12px; margin-top: 20px; padding-top: 14px; border-top: 1px solid var(--border); }
.modal-foot .spacer { flex: 1; }
@media (max-width: 600px) { .form-grid { grid-template-columns: 1fr; } }
