:root {
    color-scheme: light dark;
    --bg: #f5f6f8;
    --surface: #ffffff;
    --text: #1d2433;
    --muted: #667085;
    --border: #d9dde5;
    --primary: #1f6f5c;
    --primary-text: #ffffff;
    --income: #177245;
    --expense: #b42318;
    --warning-bg: #fff6e0;
    --warning-text: #7a5300;
    --error-bg: #fdecea;
    --success-bg: #e6f5ec;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14171f;
        --surface: #1e222c;
        --text: #eceef2;
        --muted: #a1a8b5;
        --border: #363c49;
        --primary: #4cb79c;
        --primary-text: #0d1512;
        --income: #5fce95;
        --expense: #f58a80;
        --warning-bg: #3a2f12;
        --warning-text: #f3d48a;
        --error-bg: #3f1f1c;
        --success-bg: #173526;
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 16px/1.5 "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--primary); }
h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.1rem; margin: 0 0 12px; }
h3 { font-size: 1rem; margin: 0 0 12px; }

[hidden] { display: none !important; }

.topbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 20px;
    padding: 12px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.topbar nav { display: flex; flex-wrap: wrap; gap: 4px 16px; flex: 1; }
.topbar nav a { text-decoration: none; color: var(--text); }
.topbar nav a:hover, .topbar nav a[aria-current="page"] { color: var(--primary); }
.topbar nav a[aria-current="page"] { font-weight: 600; }
.tab-icon { display: none; }
.brand { font-weight: 700; text-decoration: none; }

.container { max-width: 1000px; margin: 0 auto; padding: 20px 16px 48px; }
.page-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: end; gap: 12px; margin-bottom: 16px; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 16px;
    margin-bottom: 16px;
}
.login-card { max-width: 380px; margin: 10vh auto 0; }
.login-card h1 { margin-bottom: 16px; }
.login-card .button { margin-top: 14px; width: 100%; }

label { display: block; margin-bottom: 4px; font-size: 0.875rem; color: var(--muted); }

input, select, textarea {
    width: 100%;
    padding: 9px 10px;
    border: 1px solid var(--border);
    border-radius: 7px;
    background: var(--surface);
    color: var(--text);
    font: inherit;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 1px;
}
.is-flagged { border-color: var(--warning-text); background: var(--warning-bg); }

.button {
    display: inline-block;
    padding: 9px 16px;
    border: 1px solid var(--primary);
    border-radius: 7px;
    background: var(--primary);
    color: var(--primary-text);
    font: inherit;
    font-weight: 600;
    text-decoration: none;
    cursor: pointer;
}
.button:disabled { opacity: 0.6; cursor: not-allowed; }
.button-secondary { background: transparent; color: var(--primary); }
label.button { margin: 0; }

.link-button {
    padding: 0;
    border: 0;
    background: none;
    color: var(--primary);
    font: inherit;
    text-decoration: underline;
    cursor: pointer;
}
.danger { color: var(--expense); }

.alert { padding: 10px 12px; border-radius: 7px; margin: 0 0 14px; }
.alert-success { background: var(--success-bg); }
.alert-error { background: var(--error-bg); }
.alert-warning { background: var(--warning-bg); color: var(--warning-text); }

.field-error { margin: 4px 0 0; color: var(--expense); font-size: 0.875rem; }
.muted { color: var(--muted); }

.drop-zone {
    margin-top: 12px;
    padding: 16px;
    border: 1px dashed var(--border);
    border-radius: 8px;
    text-align: center;
    cursor: pointer;
}
.drop-zone.is-dragging { border-color: var(--primary); background: var(--success-bg); }
.drop-zone input { margin-top: 8px; border: 0; padding: 0; }

.actions { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-top: 14px; }
.review { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--border); }
.receipt-preview { display: block; max-width: 220px; max-height: 280px; margin-bottom: 14px; border-radius: 6px; border: 1px solid var(--border); }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.filters { display: flex; flex-wrap: wrap; align-items: end; gap: 12px; margin-bottom: 14px; }
.filters > div { flex: 1 1 170px; }
.filters > .range-picker { flex: 2 1 240px; position: relative; }
.range-toggle {
    width: 100%;
    padding: 9px 10px;
    border: 1px solid var(--border);
    border-radius: 7px;
    background: var(--surface);
    color: var(--text);
    font: inherit;
    text-align: left;
    cursor: pointer;
}
.range-popup {
    position: absolute;
    z-index: 10;
    top: 100%;
    left: 0;
    width: 300px;
    max-width: 100%;
    margin-top: 4px;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}
.range-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; font-weight: 600; }
.range-nav { min-width: 32px; min-height: 32px; font-size: 1.25rem; text-decoration: none; }
.range-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.range-weekday { padding: 4px 0; color: var(--muted); font-size: 0.8rem; text-align: center; }
.range-day {
    min-height: 34px;
    padding: 0;
    border: 0;
    border-radius: 6px;
    background: none;
    color: var(--text);
    font: inherit;
    cursor: pointer;
}
.range-day:hover { background: var(--border); }
.range-day.is-between { background: var(--success-bg); border-radius: 0; }
.range-day.is-end { background: var(--primary); color: var(--primary-text); font-weight: 600; }
.range-hint { margin: 8px 0; font-size: 0.8rem; }
.range-shortcuts { display: flex; justify-content: space-between; gap: 12px; }

.inline-form { display: flex; align-items: center; gap: 8px; }
.inline-form label { margin: 0; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
.stat span { display: block; color: var(--muted); font-size: 0.875rem; }
.stat strong { font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.income { color: var(--income); }
.expense { color: var(--expense); }

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.grid-2 .card { margin-bottom: 0; }

.bar-row { margin-bottom: 12px; }
.bar-label { display: flex; justify-content: space-between; gap: 12px; font-size: 0.925rem; }
.bar-track { height: 8px; border-radius: 4px; background: var(--border); overflow: hidden; }
.bar-fill { height: 100%; background: var(--primary); }

.table-scroll { position: relative; overflow-x: auto; } /* relative keeps .sr-only headings inside the scroll area */
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 8px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { font-size: 0.875rem; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: 0; }
.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.nowrap { white-space: nowrap; }
.row-actions { display: flex; justify-content: flex-end; gap: 12px; white-space: nowrap; }
/* On phones, keep the amount visible; category and account remain on the edit page. */
@media (max-width: 600px) {
    .table-scroll th:nth-child(3), .table-scroll td:nth-child(3),
    .table-scroll th:nth-child(4), .table-scroll td:nth-child(4) { display: none; }
}
.pagination { display: flex; justify-content: center; gap: 16px; margin-top: 14px; }

.spinner-wrap { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); }
.spinner {
    width: 18px;
    height: 18px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

/* Phone / installed-app layout: sticky top bar, bottom tab bar, larger touch targets, notch-safe padding. */
@media (max-width: 600px) {
    html { -webkit-tap-highlight-color: transparent; }

    .topbar {
        position: sticky;
        top: 0;
        z-index: 20;
        flex-wrap: nowrap;
        justify-content: space-between;
        padding: calc(10px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) 10px calc(16px + env(safe-area-inset-left));
    }
    .brand { font-size: 1.1rem; }

    .topbar .tabbar {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 20;
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 0;
        padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
        background: var(--surface);
        border-top: 1px solid var(--border);
    }
    .topbar .tabbar a {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        min-height: 58px;
        padding: 6px 2px;
        color: var(--muted);
        font-size: 0.75rem;
    }
    .topbar .tabbar a[aria-current="page"] { color: var(--primary); }
    .tab-icon {
        display: block;
        width: 24px;
        height: 24px;
        fill: none;
        stroke: currentColor;
        stroke-width: 2;
        stroke-linecap: round;
        stroke-linejoin: round;
    }

    .container {
        padding: 16px calc(12px + env(safe-area-inset-right)) calc(90px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left));
    }
    h1 { font-size: 1.3rem; }
    .card { border-radius: 14px; padding: 14px; }

    input, select, .range-toggle, .button { min-height: 44px; }
    .actions .button { flex: 1 1 auto; text-align: center; }

    .stats { grid-template-columns: repeat(2, 1fr); gap: 10px; }
    .stats .stat:last-child:nth-child(odd) { grid-column: 1 / -1; }
    .stat { padding: 12px; }
    .stat strong { font-size: 1.1rem; }

    .range-popup { width: 100%; }
    .row-actions { gap: 16px; }
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
}
