:root {
    --bg-deep: #0c1222;
    --bg-card: #141c2f;
    --bg-elevated: #1a2438;
    --border: rgba(255, 255, 255, 0.08);
    --text: #e8ecf4;
    --text-muted: #8b95a8;
    --accent: #3dcea6;
    --accent-dim: rgba(61, 206, 166, 0.15);
    --danger: #f07178;
    --danger-dim: rgba(240, 113, 120, 0.15);
    --income: #7dd3a8;
    --expense: #f0a8a8;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
    --font: "DM Sans", system-ui, sans-serif;
}

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

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    font-family: var(--font);
    color: var(--text);
    background: var(--bg-deep);
    line-height: 1.5;
    padding-left: env(safe-area-inset-left, 0);
    padding-right: env(safe-area-inset-right, 0);
}

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

.app-bg {
    position: fixed;
    inset: 0;
    z-index: -1;
    background:
        radial-gradient(ellipse 80% 50% at 20% -10%, rgba(61, 206, 166, 0.12), transparent),
        radial-gradient(ellipse 60% 40% at 100% 0%, rgba(100, 120, 255, 0.08), transparent),
        var(--bg-deep);
}

.topbar {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem 1.5rem;
    padding: 0.75rem 1rem;
    padding-top: max(0.75rem, env(safe-area-inset-top, 0));
    padding-left: max(1rem, env(safe-area-inset-left, 0));
    padding-right: max(1rem, env(safe-area-inset-right, 0));
    border-bottom: 1px solid var(--border);
    background: rgba(12, 18, 34, 0.92);
    backdrop-filter: blur(12px);
    position: sticky;
    top: 0;
    z-index: 100;
}

.topbar--auth {
    display: grid;
    grid-template-columns: 1fr auto auto;
    grid-template-areas:
        "brand toggle user"
        "nav nav nav";
    align-items: center;
    gap: 0.5rem 0.75rem;
    column-gap: 0.75rem;
}

.topbar--auth .brand {
    grid-area: brand;
    min-width: 0;
}

.topbar--auth .nav-toggle {
    grid-area: toggle;
}

.topbar--auth .user-menu {
    grid-area: user;
}

.topbar--auth .nav {
    grid-area: nav;
}

.nav-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    margin: 0;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
    color: var(--text);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.nav-toggle-box {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    width: 20px;
}

.nav-toggle-bar {
    display: block;
    height: 2px;
    background: currentColor;
    border-radius: 1px;
    transition: transform 0.2s, opacity 0.2s;
}

.topbar--auth.nav-is-open .nav-toggle-bar:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
}

.topbar--auth.nav-is-open .nav-toggle-bar:nth-child(2) {
    opacity: 0;
    transform: scaleX(0);
}

.topbar--auth.nav-is-open .nav-toggle-bar:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
    font-weight: 700;
    font-size: 1.0625rem;
    color: var(--text);
    text-decoration: none;
    letter-spacing: -0.02em;
}

.brand-logo {
    width: 32px;
    height: 32px;
    object-fit: contain;
    border-radius: 8px;
    flex-shrink: 0;
}

.brand-text {
    min-width: 0;
}

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

.brand:hover .brand-logo {
    opacity: 0.95;
}

.nav {
    display: none;
    flex-direction: column;
    gap: 0.25rem;
    width: 100%;
    padding: 0.35rem 0 0.5rem;
    border-top: 1px solid var(--border);
    margin: 0;
}

.topbar--auth.nav-is-open .nav {
    display: flex;
}

.nav a {
    color: var(--text-muted);
    text-decoration: none;
    padding: 0.75rem 0.5rem;
    min-height: 44px;
    display: flex;
    align-items: center;
    border-radius: var(--radius-sm);
    font-weight: 500;
    font-size: 1rem;
    transition: color 0.15s, background 0.15s;
    -webkit-tap-highlight-color: transparent;
}

.nav a:hover,
.nav a:focus-visible {
    color: var(--text);
    background: var(--bg-elevated);
}

.nav a.active {
    color: var(--accent);
    background: var(--accent-dim);
}

.user-menu {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
}

.username {
    font-size: 0.8125rem;
    color: var(--text-muted);
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.main {
    max-width: 1100px;
    margin: 0 auto;
    padding: 1.25rem 1rem 5rem;
    padding-bottom: max(5rem, calc(4rem + env(safe-area-inset-bottom, 0)));
}

.footer {
    text-align: center;
    padding: 1rem 1rem max(1rem, env(safe-area-inset-bottom, 0));
    color: var(--text-muted);
    font-size: 0.8125rem;
    border-top: 1px solid var(--border);
}

.footer-inner {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.35rem;
}

@media (min-width: 480px) {
    .footer-inner {
        flex-direction: row;
        justify-content: center;
        flex-wrap: wrap;
        gap: 0.5rem 1rem;
    }
}

.footer-app {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    color: var(--text-muted);
}

.footer-logo {
    width: 22px;
    height: 22px;
    object-fit: contain;
    border-radius: 5px;
    opacity: 0.9;
    flex-shrink: 0;
}

.auth-logo {
    display: block;
    width: 72px;
    height: 72px;
    margin: 0 auto 1rem;
    object-fit: contain;
    border-radius: 16px;
}

.footer-credit {
    font-size: 0.8125rem;
}

.footer-credit a {
    color: var(--accent);
    font-weight: 600;
    text-decoration: none;
}

.footer-credit a:hover {
    text-decoration: underline;
}

.section-heading {
    font-size: 1.15rem;
    font-weight: 700;
    margin: 2rem 0 0.35rem;
    letter-spacing: -0.02em;
    color: var(--text);
}

.section-lead {
    margin: 0 0 1.25rem;
    font-size: 0.9375rem;
    color: var(--text-muted);
}

/* Reports: month calendar */
.cal-card .cal-grid {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    gap: 4px;
    margin-bottom: 1.25rem;
}

.cal-dow {
    font-size: 0.6875rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    text-align: center;
    padding: 0.35rem 0;
}

.cal-pad {
    min-height: 1px;
}

.cal-day {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: 2px;
    min-height: 3.25rem;
    padding: 0.35rem 0.2rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
    color: var(--text);
    font-family: inherit;
    font-size: 0.8125rem;
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
    -webkit-tap-highlight-color: transparent;
}

.cal-day:hover {
    border-color: rgba(61, 206, 166, 0.45);
    background: var(--bg-card);
}

.cal-day--spend {
    border-color: rgba(240, 168, 168, 0.35);
}

.cal-day--selected {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-dim);
    background: var(--accent-dim);
}

.cal-daynum {
    font-weight: 700;
    font-size: 0.9375rem;
    line-height: 1.2;
}

.cal-spend {
    font-size: 0.65rem;
    font-weight: 600;
    color: var(--expense);
    text-align: center;
    line-height: 1.15;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.cal-spend--muted {
    color: var(--text-muted);
    font-weight: 500;
}

.cal-detail {
    margin-top: 0.5rem;
    padding: 1rem 1.1rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-elevated);
}

.cal-detail-title {
    margin: 0 0 0.75rem;
    font-size: 1.05rem;
    font-weight: 600;
    color: var(--text);
}

.cal-detail-summary {
    margin: 0 0 0.65rem;
    font-size: 0.9375rem;
    color: var(--text-muted);
}

.cal-detail-summary strong {
    color: var(--text);
}

.cal-detail-summary--expense strong {
    color: var(--expense);
}

.cal-detail-summary--income strong {
    color: var(--income);
}

.cal-detail-sub {
    margin: 1rem 0 0.5rem;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.cal-detail-sub:first-of-type {
    margin-top: 0.25rem;
}

.cal-detail-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.cal-detail-list li {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.35rem 0.75rem;
    padding: 0.55rem 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.875rem;
    align-items: baseline;
}

.cal-detail-list li:last-child {
    border-bottom: none;
}

.cal-li-cat {
    color: var(--text);
    font-weight: 500;
}

.cal-li-amt {
    font-weight: 700;
    justify-self: end;
}

.cal-li-note {
    grid-column: 1 / -1;
    font-size: 0.8125rem;
    color: var(--text-muted);
}

.cal-detail-empty {
    margin: 0;
    color: var(--text-muted);
    font-size: 0.9375rem;
}

@media (min-width: 640px) {
    .cal-day {
        min-height: 3.75rem;
        padding: 0.45rem 0.35rem;
    }

    .cal-spend {
        font-size: 0.7rem;
    }
}

h1 {
    font-size: clamp(1.35rem, 4vw + 0.5rem, 1.75rem);
    font-weight: 700;
    margin: 0 0 0.25rem;
    letter-spacing: -0.03em;
    line-height: 1.2;
}

.subtitle {
    color: var(--text-muted);
    margin: 0 0 1.75rem;
    font-size: 0.9375rem;
}

.grid {
    display: grid;
    gap: 1.25rem;
}

@media (min-width: 768px) {
    .grid-3 {
        grid-template-columns: repeat(3, 1fr);
    }

    .grid-charts {
        grid-template-columns: repeat(2, 1fr);
    }
}

.grid-charts {
    display: grid;
    gap: 1.25rem;
    grid-template-columns: 1fr;
}

.chart-card .chart-caption {
    font-size: 0.8125rem;
    color: var(--text-muted);
    margin: 0 0 0.75rem;
}

.chart-wrap {
    position: relative;
    width: 100%;
}

.chart-wrap--doughnut {
    height: 240px;
    max-height: 55vmin;
}

.chart-wrap--bar {
    height: min(320px, 70vw);
    min-height: 200px;
}

.chart-wrap--line {
    height: 280px;
    max-height: 60vmin;
}

.chart-wrap--line-wide {
    height: min(320px, 70vw);
    min-height: 240px;
}

@media (min-width: 720px) {
    .grid-2 {
        grid-template-columns: repeat(2, 1fr);
    }
}

.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.35rem 1.5rem;
    box-shadow: var(--shadow);
}

.card h2 {
    font-size: 0.8125rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    margin: 0 0 0.5rem;
}

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

.stat-value.income {
    color: var(--income);
}

.stat-value.expense {
    color: var(--expense);
}

.stat-value.balance.positive {
    color: var(--income);
}

.stat-value.balance.negative {
    color: var(--expense);
}

.form-group {
    margin-bottom: 1.15rem;
}

.form-group label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 0.4rem;
    color: var(--text-muted);
}

input[type="text"],
input[type="password"],
input[type="number"],
input[type="date"],
select,
textarea {
    width: 100%;
    padding: 0.65rem 0.85rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-elevated);
    color: var(--text);
    font-family: inherit;
    font-size: 1rem;
}

input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-dim);
}

textarea {
    min-height: 88px;
    resize: vertical;
}

input[type="number"] {
    -moz-appearance: textfield;
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    min-height: 44px;
    padding: 0.65rem 1.25rem;
    border-radius: var(--radius-sm);
    font-family: inherit;
    font-size: 0.9375rem;
    font-weight: 600;
    border: none;
    cursor: pointer;
    text-decoration: none;
    transition: transform 0.1s, opacity 0.15s;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.btn:active {
    transform: scale(0.98);
}

.btn-primary {
    background: linear-gradient(135deg, #3dcea6, #2eb88f);
    color: #0a1628;
}

.btn-primary:hover {
    opacity: 0.95;
}

.btn-ghost {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
}

.btn-ghost:hover {
    color: var(--text);
    background: var(--bg-elevated);
}

.btn-sm {
    min-height: 40px;
    padding: 0.45rem 0.85rem;
    font-size: 0.8125rem;
}

.toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: stretch;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.toolbar .form-group {
    margin-bottom: 0;
    flex: 1 1 140px;
    min-width: 0;
}

.toolbar .btn {
    flex: 1 1 auto;
    min-width: 120px;
}

.alert {
    padding: 0.85rem 1rem;
    border-radius: var(--radius-sm);
    margin-bottom: 1.25rem;
    font-size: 0.9375rem;
}

.alert-success {
    background: var(--accent-dim);
    color: var(--accent);
    border: 1px solid rgba(61, 206, 166, 0.35);
}

.alert-error {
    background: var(--danger-dim);
    color: var(--danger);
    border: 1px solid rgba(240, 113, 120, 0.35);
}

.table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    margin: 0 -0.25rem;
    padding: 0 0.25rem;
}

@media (min-width: 640px) {
    .table-wrap {
        margin: 0;
        padding: 0;
    }
}

table.data {
    width: 100%;
    min-width: 520px;
    border-collapse: collapse;
    font-size: 0.875rem;
}

table.data th,
table.data td {
    padding: 0.65rem 0.75rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

@media (min-width: 640px) {
    table.data {
        font-size: 0.9375rem;
    }

    table.data th,
    table.data td {
        padding: 0.75rem 1rem;
    }
}

table.data th {
    background: var(--bg-elevated);
    color: var(--text-muted);
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

table.data tr:last-child td {
    border-bottom: none;
}

table.data .amt-income {
    color: var(--income);
    font-weight: 600;
}

table.data .amt-expense {
    color: var(--expense);
    font-weight: 600;
}

.badge {
    display: inline-block;
    padding: 0.2rem 0.55rem;
    border-radius: 6px;
    font-size: 0.75rem;
    font-weight: 600;
}

.badge-income {
    background: var(--accent-dim);
    color: var(--accent);
}

.badge-expense {
    background: var(--danger-dim);
    color: var(--danger);
}

.empty-state {
    text-align: center;
    padding: 2.5rem 1rem;
    color: var(--text-muted);
}

.report-bar {
    margin-bottom: 0.85rem;
}

.report-bar .label {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.5rem;
    font-size: 0.875rem;
    margin-bottom: 0.35rem;
}

.report-bar .label span:last-child {
    flex-shrink: 0;
}

.report-bar .track {
    height: 8px;
    background: var(--bg-elevated);
    border-radius: 4px;
    overflow: hidden;
}

.report-bar .fill {
    height: 100%;
    background: linear-gradient(90deg, #3dcea6, #5ae0bb);
    border-radius: 4px;
    min-width: 4px;
}

.report-bar.expense .fill {
    background: linear-gradient(90deg, #e8787e, #f0a8a8);
}

.auth-layout {
    min-height: calc(100dvh - 100px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem 1rem;
    padding-bottom: max(1.5rem, env(safe-area-inset-bottom, 0));
}

.auth-card {
    width: 100%;
    max-width: 400px;
}

.auth-card h1 {
    text-align: center;
    margin-bottom: 0.5rem;
}

.auth-card .subtitle {
    text-align: center;
}

.auth-card .card {
    margin-top: 1.5rem;
}

.auth-switch {
    text-align: center;
    margin-top: 1.25rem;
    font-size: 0.9375rem;
    color: var(--text-muted);
}

.auth-switch a {
    color: var(--accent);
    font-weight: 600;
    text-decoration: none;
}

.auth-switch a:hover {
    text-decoration: underline;
}

.row-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

@media (max-width: 480px) {
    .row-actions {
        flex-direction: column;
    }

    .row-actions .btn {
        width: 100%;
    }
}

/* Desktop navigation: horizontal bar */
@media (min-width: 900px) {
    .topbar {
        flex-wrap: nowrap;
        padding: 1rem 1.5rem;
        padding-left: max(1.5rem, env(safe-area-inset-left, 0));
        padding-right: max(1.5rem, env(safe-area-inset-right, 0));
    }

    .topbar--auth {
        display: flex;
        flex-wrap: nowrap;
        align-items: center;
        gap: 1rem 1.5rem;
    }

    .topbar--auth .brand {
        flex: 0 0 auto;
    }

    .nav-toggle {
        display: none;
    }

    .topbar--auth .nav {
        display: flex !important;
        flex-direction: row;
        flex-wrap: wrap;
        flex: 1;
        justify-content: center;
        width: auto;
        padding: 0;
        border-top: none;
        gap: 0.25rem 0.5rem;
    }

    .nav a {
        padding: 0.5rem 0.85rem;
        min-height: auto;
        font-size: 0.9375rem;
    }

    .topbar--auth .user-menu {
        flex: 0 0 auto;
    }

    .username {
        max-width: 200px;
    }

    .main {
        padding: 2rem 1.5rem 4rem;
    }

    .brand {
        font-size: 1.125rem;
    }
}
