/*
 * Mis Finanzas - single hand-written stylesheet (no framework, no build step).
 * Mobile-first: base rules target a phone; the 900px breakpoint moves the
 * bottom tab bar into a top navigation for wide screens.
 */

/* ---------- Design tokens ---------- */
:root {
    color-scheme: light dark;

    --bg: #f4f5f7;
    --surface: #ffffff;
    --surface-2: #eef0f3;
    --text: #15171c;
    --text-muted: #5b6270;
    --border: #e1e4e9;
    --accent: #4b45d6;
    --accent-strong: #3a34b8;
    --accent-soft: #ebeafd;
    --on-accent: #ffffff;
    --income: #0b7d55;
    --income-soft: #e3f4ec;
    --expense: #c7392f;
    --expense-soft: #fbe9e7;
    --warning-bg: #fff6e0;
    --warning-text: #7a5300;
    --focus: #4b45d6;

    /* Doughnut palette, read by app.js so the chart follows the theme. */
    --chart-1: #4b45d6;
    --chart-2: #e8705f;
    --chart-3: #1c9a6c;
    --chart-4: #e0a526;
    --chart-5: #3a8fd6;
    --chart-6: #a35fc9;
    --chart-7: #6f7787;
    --chart-8: #d4568c;

    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;
    --radius-sm: 10px;
    --radius: 16px;
    --radius-pill: 999px;
    --shadow: 0 1px 2px rgba(16, 18, 23, 0.04), 0 4px 16px rgba(16, 18, 23, 0.06);
    --tap: 44px;
    --tabbar-height: 64px;
    --topbar-height: 56px;
    --page-max: 960px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #101217;
        --surface: #1a1d24;
        --surface-2: #242832;
        --text: #eceef2;
        --text-muted: #a1a8b5;
        --border: #2c313b;
        --accent: #8f8aff;
        --accent-strong: #aaa6ff;
        --accent-soft: #272653;
        --on-accent: #12121f;
        --income: #43d39b;
        --income-soft: #133227;
        --expense: #ff8577;
        --expense-soft: #3a1d1b;
        --warning-bg: #3a2e10;
        --warning-text: #f5d17a;
        --focus: #aaa6ff;

        --chart-1: #8f8aff;
        --chart-2: #ff8f7d;
        --chart-3: #43d39b;
        --chart-4: #f2c14e;
        --chart-5: #5eb2f5;
        --chart-6: #c58af0;
        --chart-7: #a1a8b5;
        --chart-8: #f57fb0;

        --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.25);
    }
}

/* ---------- Base ---------- */
*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
    padding-bottom: calc(var(--tabbar-height) + env(safe-area-inset-bottom) + var(--space-4));
}

h1, h2, h3, p {
    margin: 0;
}

a {
    color: var(--accent);
}

img,
svg {
    display: block;
}

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

.visually-hidden {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.skip-link {
    position: absolute;
    left: var(--space-4);
    top: -100px;
    z-index: 100;
    padding: var(--space-2) var(--space-4);
    background: var(--surface);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
}

.skip-link:focus {
    top: calc(env(safe-area-inset-top) + var(--space-2));
}

.muted {
    color: var(--text-muted);
}

.amount {
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.amount-income {
    color: var(--income);
}

.amount-expense {
    color: var(--expense);
}

.currency {
    font-size: 0.7em;
    font-weight: 600;
    color: var(--text-muted);
    letter-spacing: 0.02em;
}

/* ---------- Top bar ---------- */
.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    min-height: calc(var(--topbar-height) + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) max(var(--space-4), env(safe-area-inset-right)) 0 max(var(--space-4), env(safe-area-inset-left));
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    -webkit-backdrop-filter: saturate(180%) blur(16px);
    backdrop-filter: saturate(180%) blur(16px);
    border-bottom: 1px solid var(--border);
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--tap);
    color: var(--text);
    font-weight: 700;
    font-size: 1.05rem;
    text-decoration: none;
}

.brand-mark {
    width: 28px;
    height: 28px;
    border-radius: 8px;
}

.locale-switch {
    display: inline-flex;
    padding: 3px;
    background: var(--surface-2);
    border-radius: var(--radius-pill);
}

.locale-switch a {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: var(--tap);
    min-height: 38px;
    padding: 0 var(--space-3);
    border-radius: var(--radius-pill);
    color: var(--text-muted);
    font-size: 0.85rem;
    font-weight: 600;
    text-decoration: none;
}

.locale-switch a.is-active {
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
}

/* ---------- Bottom tab bar (mobile) ---------- */
.tabbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    align-items: end;
    min-height: calc(var(--tabbar-height) + env(safe-area-inset-bottom));
    padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    -webkit-backdrop-filter: saturate(180%) blur(16px);
    backdrop-filter: saturate(180%) blur(16px);
    border-top: 1px solid var(--border);
}

.tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-height: var(--tabbar-height);
    padding: var(--space-1) 2px;
    color: var(--text-muted);
    font-size: 0.68rem;
    font-weight: 600;
    line-height: 1.2;
    text-align: center;
    text-decoration: none;
}

.tab-icon {
    width: 24px;
    height: 24px;
    fill: currentColor;
}

.tab.is-active {
    color: var(--accent);
}

.tab-primary-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 52px;
    height: 52px;
    margin-top: -22px;
    border-radius: 50%;
    background: var(--accent);
    color: var(--on-accent);
    box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 40%, transparent);
    border: 4px solid var(--bg);
}

.tab-primary.is-active .tab-primary-badge {
    background: var(--accent-strong);
}

/* ---------- Page layout ---------- */
.page {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    width: 100%;
    max-width: var(--page-max);
    margin: 0 auto;
    padding: var(--space-4) max(var(--space-4), env(safe-area-inset-right)) var(--space-4) max(var(--space-4), env(safe-area-inset-left));
}

.page-narrow {
    max-width: 560px;
}

.page-title {
    font-size: 1.6rem;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.card-title {
    font-size: 1rem;
    font-weight: 700;
}

/* ---------- Buttons ---------- */
.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: var(--tap);
    padding: 0 var(--space-5);
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    font: inherit;
    font-weight: 600;
    text-decoration: none;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease;
}

.button-primary {
    background: var(--accent);
    color: var(--on-accent);
}

.button-primary:hover {
    background: var(--accent-strong);
}

.button-secondary {
    background: var(--accent-soft);
    color: var(--accent-strong);
}

.button-ghost {
    background: transparent;
    color: var(--text);
    border-color: var(--border);
}

.button-ghost:hover {
    background: var(--surface-2);
}

.button-danger-ghost {
    background: transparent;
    color: var(--expense);
    border-color: transparent;
}

.button-danger-ghost:hover {
    background: var(--expense-soft);
}

.button-small {
    padding: 0 var(--space-3);
    font-size: 0.9rem;
}

.icon-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--tap);
    height: var(--tap);
    border-radius: 50%;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
}

.icon-button svg {
    width: 24px;
    height: 24px;
    fill: currentColor;
}

.actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--space-3);
}

/* ---------- Notices ---------- */
.notice {
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-sm);
    font-weight: 500;
}

.notice-warning {
    background: var(--warning-bg);
    color: var(--warning-text);
}

.notice-error {
    background: var(--expense-soft);
    color: var(--expense);
}

/* ---------- Dashboard ---------- */
.period-bar {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-3);
}

.period-picker {
    flex: 1;
    text-align: center;
}

.period-picker summary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--tap);
    padding: 0 var(--space-3);
    border-radius: var(--radius-sm);
    list-style: none;
    cursor: pointer;
}

.period-picker summary::-webkit-details-marker {
    display: none;
}

.period-picker summary::after {
    content: "";
    width: 8px;
    height: 8px;
    margin-left: var(--space-2);
    border-right: 2px solid var(--text-muted);
    border-bottom: 2px solid var(--text-muted);
    transform: translateY(-2px) rotate(45deg);
}

.period-picker[open] summary::after {
    transform: translateY(2px) rotate(-135deg);
}

.period-label {
    font-size: 1.2rem;
    font-weight: 700;
    text-transform: capitalize;
}

.period-form {
    display: grid;
    gap: var(--space-2);
    margin-top: var(--space-2);
    padding: var(--space-4);
    text-align: left;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.period-form label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
}

.inline-field {
    display: flex;
    gap: var(--space-2);
}

.inline-field .input,
.inline-field input[type="month"] {
    flex: 1;
    min-width: 0;
}

.chips {
    display: flex;
    gap: var(--space-2);
    overflow-x: auto;
    padding-bottom: 2px;
    scrollbar-width: none;
}

.chip {
    display: inline-flex;
    align-items: center;
    min-height: 36px;
    padding: 0 var(--space-4);
    border-radius: var(--radius-pill);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 0.9rem;
    font-weight: 600;
    text-decoration: none;
    /* Enlarge the hit area to 44px without changing the visual size. */
    position: relative;
}

.chip::after {
    content: "";
    position: absolute;
    inset: -4px 0;
}

.chip.is-active {
    background: var(--text);
    color: var(--bg);
    border-color: var(--text);
}

.summary {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-3);
}

.stat {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    padding: var(--space-4);
}

.stat-label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.stat-value {
    font-size: 1.35rem;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.stat-balance {
    grid-column: 1 / -1;
    background: var(--accent);
    border-color: transparent;
    color: var(--on-accent);
}

.stat-balance .stat-label,
.stat-balance .currency {
    color: color-mix(in srgb, var(--on-accent) 75%, transparent);
}

.stat-balance .stat-value {
    font-size: 2.1rem;
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-6) var(--space-4);
    text-align: center;
}

.empty-state h2 {
    font-size: 1.1rem;
}

.empty-state p {
    color: var(--text-muted);
    max-width: 36ch;
}

.breakdown {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    padding: var(--space-4);
}

.breakdown-body {
    display: grid;
    gap: var(--space-4);
}

.chart-wrap {
    position: relative;
    width: min(260px, 100%);
    margin: 0 auto;
}

.legend {
    display: grid;
    gap: var(--space-1);
    margin: 0;
    padding: 0;
    list-style: none;
}

.legend li {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: 36px;
    border-bottom: 1px solid var(--border);
}

.legend li:last-child {
    border-bottom: 0;
}

.swatch {
    flex: none;
    width: 12px;
    height: 12px;
    border-radius: 4px;
    background: var(--chart-1);
}

.legend li:nth-child(8n + 2) .swatch { background: var(--chart-2); }
.legend li:nth-child(8n + 3) .swatch { background: var(--chart-3); }
.legend li:nth-child(8n + 4) .swatch { background: var(--chart-4); }
.legend li:nth-child(8n + 5) .swatch { background: var(--chart-5); }
.legend li:nth-child(8n + 6) .swatch { background: var(--chart-6); }
.legend li:nth-child(8n + 7) .swatch { background: var(--chart-7); }
.legend li:nth-child(8n) .swatch { background: var(--chart-8); }

.legend-name {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.legend-value {
    font-weight: 600;
}

/* ---------- Ledger ---------- */
.ledger {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin: 0;
    padding: 0;
    list-style: none;
}

.ledger-date {
    padding: var(--space-3) var(--space-1) 0;
    color: var(--text-muted);
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.ledger-date:first-child {
    padding-top: 0;
}

.ledger-item {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
        "main amount"
        "actions actions";
    gap: var(--space-2) var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-left: 4px solid var(--expense);
}

.ledger-item.is-income {
    border-left-color: var(--income);
}

.ledger-main {
    grid-area: main;
    min-width: 0;
}

.ledger-concept {
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ledger-meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
    margin-top: 2px;
    color: var(--text-muted);
    font-size: 0.85rem;
}

.pill {
    display: inline-block;
    max-width: 100%;
    padding: 1px var(--space-2);
    border-radius: var(--radius-pill);
    background: var(--accent-soft);
    color: var(--accent-strong);
    font-size: 0.78rem;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ledger-amount {
    grid-area: amount;
    align-self: center;
    font-size: 1.1rem;
    font-weight: 700;
}

.ledger-actions {
    grid-area: actions;
    display: flex;
    justify-content: flex-end;
    gap: var(--space-1);
    margin: 0 calc(-1 * var(--space-2)) calc(-1 * var(--space-1));
}

.ledger-actions form {
    margin: 0;
}

/* ---------- Forms ---------- */
.form {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    padding: var(--space-5) var(--space-4);
}

.field {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    margin: 0;
    padding: 0;
    border: 0;
    min-width: 0;
}

.field-row {
    display: flex;
    gap: var(--space-3);
}

.field-grow {
    flex: 1;
}

.field-currency {
    width: 96px;
}

.field-label {
    padding: 0;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
}

.input,
input[type="month"] {
    width: 100%;
    min-height: 48px;
    padding: 0 var(--space-3);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    /* 16px+ keeps iOS Safari from zooming into focused inputs. */
    font: inherit;
    font-size: 1rem;
    -webkit-appearance: none;
    appearance: none;
}

.input:focus,
input[type="month"]:focus {
    border-color: var(--accent);
    outline: 3px solid color-mix(in srgb, var(--accent) 30%, transparent);
    outline-offset: 0;
}

.input-amount {
    font-size: 1.25rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

#currency {
    text-transform: uppercase;
}

.has-error .input {
    border-color: var(--expense);
}

.has-error .field-label {
    color: var(--expense);
}

.fieldError {
    color: var(--expense);
    font-size: 0.85rem;
    font-weight: 500;
}

.segmented {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-1);
    padding: var(--space-1);
    background: var(--surface-2);
    border-radius: var(--radius-sm);
}

.segment {
    position: relative;
    display: flex;
}

.segment input {
    position: absolute;
    opacity: 0;
    inset: 0;
    margin: 0;
    cursor: pointer;
}

.segment span {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: var(--tap);
    border-radius: 8px;
    font-weight: 600;
    color: var(--text-muted);
}

.segment input:checked + span {
    background: var(--surface);
    box-shadow: var(--shadow);
}

.segment-expense input:checked + span {
    color: var(--expense);
}

.segment-income input:checked + span {
    color: var(--income);
}

.segment input:focus-visible + span {
    outline: 3px solid var(--focus);
    outline-offset: 1px;
}

.form-actions {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin-top: var(--space-2);
}

.form-actions .button {
    width: 100%;
}

/* Upload */
.file-input {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
}

.dropzone {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-6) var(--space-4);
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    background: var(--surface-2);
    text-align: center;
    cursor: pointer;
}

.dropzone:hover {
    border-color: var(--accent);
}

.dropzone:has(+ .file-input:focus-visible) {
    border-color: var(--accent);
    outline: 3px solid var(--focus);
    outline-offset: 2px;
}

.dropzone-icon {
    width: 48px;
    height: 48px;
    padding: 10px;
    border-radius: 50%;
    background: var(--accent);
    fill: var(--on-accent);
}

.dropzone-title {
    font-weight: 700;
}

.dropzone-hint {
    font-size: 0.9rem;
    color: var(--text-muted);
}

.dropzone-file {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--accent);
    word-break: break-all;
}

/* Error page */
.error-state h1 {
    font-size: 1.4rem;
}

.error-icon {
    width: 56px;
    height: 56px;
    fill: var(--expense);
}

/* ---------- Wide screens ---------- */
@media (min-width: 600px) {
    .summary {
        grid-template-columns: repeat(3, 1fr);
    }

    .stat-balance {
        grid-column: auto;
    }

    .stat-balance .stat-value {
        font-size: 1.6rem;
    }

    .breakdown-body {
        grid-template-columns: 260px 1fr;
        align-items: center;
    }

    .form-actions {
        flex-direction: row-reverse;
        justify-content: flex-start;
    }

    .form-actions .button {
        width: auto;
    }

    .ledger-item {
        grid-template-columns: 1fr auto auto;
        grid-template-areas: "main amount actions";
        align-items: center;
    }

    .ledger-actions {
        margin: 0;
    }
}

@media (min-width: 900px) {
    body {
        padding-bottom: var(--space-6);
    }

    .tabbar {
        position: sticky;
        top: calc(var(--topbar-height) + env(safe-area-inset-top));
        bottom: auto;
        display: flex;
        justify-content: center;
        gap: var(--space-1);
        min-height: 0;
        padding: var(--space-2) var(--space-4);
        background: var(--surface);
        border-top: 0;
        border-bottom: 1px solid var(--border);
        z-index: 15;
    }

    .tab {
        flex-direction: row;
        gap: var(--space-2);
        min-height: var(--tap);
        padding: 0 var(--space-4);
        border-radius: var(--radius-pill);
        font-size: 0.92rem;
    }

    .tab:hover {
        background: var(--surface-2);
        color: var(--text);
    }

    .tab.is-active {
        background: var(--accent-soft);
        color: var(--accent-strong);
    }

    .tab-icon {
        width: 20px;
        height: 20px;
    }

    .tab-primary-badge {
        width: auto;
        height: auto;
        margin: 0;
        border: 0;
        background: none;
        color: inherit;
        box-shadow: none;
    }

    .tab-primary {
        order: 5;
        background: var(--accent);
        color: var(--on-accent);
    }

    .tab-primary:hover,
    .tab-primary.is-active {
        background: var(--accent-strong);
        color: var(--on-accent);
    }

    .page {
        padding-top: var(--space-6);
        gap: var(--space-5);
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        transition: none !important;
        animation: none !important;
    }
}

/* ---------- Loading state (receipt upload) ---------- */
.form-loadable {
    position: relative;
}

.spinner {
    display: inline-block;
    flex: none;
    width: 18px;
    height: 18px;
    border: 2px solid color-mix(in srgb, currentColor 30%, transparent);
    border-top-color: currentColor;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.spinner-inline {
    display: none;
}

.button.is-loading .spinner-inline {
    display: inline-block;
}

.button:disabled {
    opacity: 0.75;
    cursor: progress;
}

.spinner-large {
    width: 44px;
    height: 44px;
    border-width: 4px;
    color: var(--accent);
}

.loading-overlay {
    position: absolute;
    inset: 0;
    z-index: 5;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-4);
    padding: var(--space-5);
    border-radius: var(--radius);
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
    text-align: center;
}

.loading-overlay[hidden] {
    display: none;
}

.loading-message {
    max-width: 32ch;
    color: var(--text);
    font-weight: 600;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.35;
    }
}

@media (prefers-reduced-motion: reduce) {
    /* Overrides the global animation reset: a slow pulse instead of spinning. */
    .spinner {
        animation: pulse 1.6s ease-in-out infinite !important;
        border-color: currentColor;
    }
}

/* With JS the upload form submits as soon as a photo is chosen, so the
   explicit submit button is redundant; without JS it stays visible. */
.js [data-loading-button] {
    display: none;
}

/* ---------- Ledger range filter and summary ---------- */
.range-bar {
    align-items: center;
}

.range-label {
    flex: 1;
    text-transform: none;
}

.period-bar .period-picker {
    align-self: center;
    text-align: center;
    text-transform: capitalize;
}

.range-picker summary {
    display: inline-flex;
    align-items: center;
    min-height: var(--tap);
    color: var(--accent);
    font-weight: 600;
    cursor: pointer;
}

.range-form {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-3);
    align-items: end;
    margin-top: var(--space-2);
    padding: var(--space-4);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.range-form .button {
    grid-column: 1 / -1;
}

.ledger-summary {
    display: grid;
    gap: var(--space-2);
}

.summary-row {
    display: grid;
    grid-template-columns: auto repeat(3, 1fr);
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
}

.summary-currency {
    font-weight: 700;
    color: var(--text-muted);
}

.summary-item {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.summary-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.summary-item .amount {
    font-weight: 700;
}

@media (min-width: 600px) {
    .range-form {
        grid-template-columns: 1fr 1fr auto;
    }

    .range-form .button {
        grid-column: auto;
    }
}

/* ---------- Insights and budgets ---------- */
.insights-link {
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 56px;
    padding: 0 var(--space-4);
    color: var(--accent-strong);
    font-weight: 700;
    text-decoration: none;
}

.insights-link svg {
    width: 24px;
    height: 24px;
    fill: currentColor;
}

.insight-card {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    padding: var(--space-4);
}

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
}

.insight-list {
    display: grid;
    gap: var(--space-2);
    margin: 0;
    padding-left: var(--space-5);
}

.insight-rows {
    display: grid;
    gap: var(--space-1);
    margin: 0;
    padding: 0;
    list-style: none;
}

.insight-row {
    padding: var(--space-2) 0;
    border-bottom: 1px solid var(--border);
}

.insight-row:last-child {
    border-bottom: 0;
}

.insight-row-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-3);
    min-height: 36px;
}

.insight-name {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    min-width: 0;
    font-weight: 600;
}

.insight-amount {
    font-weight: 700;
}

.insight-meta {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-1) var(--space-3);
    color: var(--text-muted);
    font-size: 0.85rem;
}

.budget-progress-list {
    display: grid;
    gap: var(--space-4);
    margin: 0;
    padding: 0;
    list-style: none;
}

.budget-progress {
    --state-color: var(--income);
    --state-bg: var(--income-soft);
}

.budget-progress.is-warning {
    --state-color: var(--warning-text);
    --state-bg: var(--warning-bg);
}

.budget-progress.is-over {
    --state-color: var(--expense);
    --state-bg: var(--expense-soft);
}

.budget-progress-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    margin-bottom: var(--space-1);
}

.budget-name {
    font-weight: 600;
}

/* Text label next to the color, so the state never relies on color alone. */
.budget-state {
    padding: 1px var(--space-2);
    border-radius: var(--radius-pill);
    background: var(--state-bg);
    color: var(--state-color);
    font-size: 0.78rem;
    font-weight: 700;
}

.progress {
    height: 10px;
    overflow: hidden;
    border-radius: var(--radius-pill);
    background: var(--surface-2);
}

.progress-fill {
    display: block;
    height: 100%;
    border-radius: inherit;
    background: var(--state-color);
}

.budget-figures {
    margin-top: var(--space-1);
    color: var(--text-muted);
    font-size: 0.85rem;
}

.budget-list {
    display: grid;
    gap: var(--space-2);
    margin: 0;
    padding: 0;
    list-style: none;
}

.budget-item {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: var(--space-2) var(--space-3);
    padding: var(--space-3) var(--space-4);
}

.budget-main {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.budget-edit {
    grid-column: 1 / -1;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
}

.budget-edit .input {
    flex: 1;
    min-width: 0;
}

.budget-item > form:last-child {
    grid-row: 1;
    grid-column: 2;
}

/* ---------- Public demo banner ---------- */
.demo-banner {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--space-1) var(--space-3);
    padding: calc(var(--space-2) + env(safe-area-inset-top)) var(--space-4) var(--space-2);
    background: var(--warning-bg);
    color: var(--warning-text);
    font-size: 0.85rem;
    font-weight: 600;
    text-align: center;
}

.demo-banner a {
    color: inherit;
}

/* The banner already pads for the notch, so the sticky top bar below it does not. */
.demo-banner + .topbar {
    padding-top: 0;
    min-height: var(--topbar-height);
}

/* ---------- Insights tone selector ---------- */
.page-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
}

.tone-switch form {
    display: inline-flex;
    gap: 2px;
    margin: 0;
    padding: 3px;
    background: var(--surface-2);
    border-radius: var(--radius-pill);
}

.tone-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--tap);
    height: var(--tap);
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    font-size: 1.3rem;
    line-height: 1;
    cursor: pointer;
    filter: grayscale(0.6);
    opacity: 0.7;
}

.tone-button[aria-pressed="true"] {
    background: var(--surface);
    box-shadow: var(--shadow);
    filter: none;
    opacity: 1;
}

.insights-summary {
    display: flex;
    flex-direction: column;
}

.insights-summary .insights-link {
    border-radius: var(--radius);
}

.insights-summary-body {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: 0 var(--space-4) var(--space-4);
}

.insight-summary-line {
    flex: 1 1 200px;
    color: var(--text-muted);
}
