:root {
    --bg: #f3efe6;
    --surface: #fffdf8;
    --ink: #1c1915;
    --muted: #6f675e;
    --line: #e4dccf;
    --income: #1f7a4d;
    --income-soft: #e7f4ec;
    --expense: #c44536;
    --expense-soft: #fbeceb;
    --shadow: 0 10px 30px rgba(48, 36, 18, 0.06);
    --radius: 16px;
}

* {
    box-sizing: border-box;
}

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

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    color: var(--ink);
    background:
        radial-gradient(circle at top left, #fff8ea 0, transparent 42%),
        var(--bg);
    overflow-x: clip;
    padding:
        env(safe-area-inset-top)
        env(safe-area-inset-right)
        env(safe-area-inset-bottom)
        env(safe-area-inset-left);
}

button,
input,
select {
    font: inherit;
    color: inherit;
}

.app {
    display: grid;
    grid-template-columns: 240px 1fr;
    min-height: 100vh;
}

.sidebar {
    padding: 28px 20px;
    border-right: 1px solid var(--line);
    background: rgba(255, 253, 248, 0.72);
}

.brand {
    display: flex;
    gap: 12px;
    align-items: center;
    margin-bottom: 32px;
}

.brand-mark {
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    border-radius: 12px;
    background: var(--ink);
    color: #f7f1e6;
    font-size: 22px;
}

.brand h1,
.page-head h2,
.panel h3,
.modal-card h3 {
    margin: 0;
    font-weight: 650;
    letter-spacing: -0.03em;
}

.brand h1 {
    font-size: 22px;
}

.brand-kicker,
.eyebrow,
.page-note,
.hint,
.stat-label,
.empty-note,
.tx-meta,
.form-error,
.form-ok {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
}

.nav {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.nav-btn {
    text-align: left;
    border: 0;
    background: transparent;
    border-radius: 10px;
    padding: 10px 12px;
    cursor: pointer;
}

.nav-btn:hover,
.nav-btn.is-active {
    background: #efe8db;
}

.content {
    min-width: 0;
    width: 100%;
    padding: clamp(24px, 2.4vw, 56px) clamp(20px, 3vw, 72px) 56px;
}

.view {
    display: none;
}

.view.is-active {
    display: block;
}

.page-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 16px;
    margin-bottom: 22px;
}

.page-head h2 {
    font-size: 32px;
    margin-top: 4px;
}

.stat-grid,
.chart-grid,
.cat-grid {
    display: grid;
    gap: 16px;
}

.stat-grid {
    grid-template-columns: repeat(3, 1fr);
    margin-bottom: 16px;
}

.chart-grid,
.cat-grid {
    grid-template-columns: 1.4fr 1fr;
}

.stat-card,
.panel,
.modal-card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.stat-card {
    padding: 18px 20px;
}

.stat-value {
    margin: 8px 0 0;
    font-size: clamp(1.35rem, 1.05rem + 1.1vw, 2.15rem);
    font-weight: 680;
    letter-spacing: -0.04em;
    line-height: 1.15;
}

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

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

.panel {
    padding: 18px 20px 20px;
}

.panel-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}

.legend,
.donut-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 10px 14px;
}

.legend-item,
.donut-legend li {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--muted);
}

.donut-legend {
    list-style: none;
    margin: 0;
    padding: 0;
    flex-direction: column;
    gap: 8px;
}

.swatch {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    display: inline-block;
}

.swatch-income {
    background: var(--income);
}

.swatch-expense {
    background: var(--expense);
}

canvas {
    width: 100%;
    height: auto;
    display: block;
}

.donut-wrap {
    display: grid;
    grid-template-columns: 180px 1fr;
    gap: 12px;
    align-items: center;
}

.filters {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 14px;
}

label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 13px;
    color: var(--muted);
}

input,
select {
    width: 100%;
    border: 1px solid var(--line);
    background: #fff;
    border-radius: 10px;
    padding: 10px 12px;
    min-width: 0;
}

input:focus,
select:focus {
    outline: 2px solid #d8c7a4;
    border-color: #c9b48a;
}

.form-panel {
    margin-bottom: 14px;
}

.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-top: 14px;
}

.span-2 {
    grid-column: span 2;
}

.form-actions {
    display: flex;
    gap: 8px;
    margin-top: 14px;
}

.btn {
    border: 0;
    border-radius: 10px;
    padding: 10px 14px;
    cursor: pointer;
    background: #efe8db;
}

.btn.primary {
    background: var(--ink);
    color: #f7f1e6;
}

.btn.danger {
    background: var(--expense);
    color: #fff;
}

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

.btn.small {
    padding: 6px 10px;
    font-size: 13px;
}

.form-error,
.form-ok {
    margin-top: 10px;
}

.form-error {
    color: var(--expense);
}

.form-ok {
    color: var(--income);
}

.tx-list,
.cat-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.tx-item,
.cat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 12px;
    padding: 12px 0;
    border-bottom: 1px solid var(--line);
}

.tx-item:last-child,
.cat-item:last-child {
    border-bottom: 0;
    padding-bottom: 0;
}

.tx-main {
    min-width: 0;
    flex: 1;
}

.tx-title,
.cat-name {
    margin: 0 0 4px;
    font-weight: 600;
    overflow-wrap: anywhere;
}

.tx-amount {
    font-weight: 700;
    white-space: nowrap;
}

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

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

.row-actions {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}

.inline-form {
    display: flex;
    gap: 8px;
    margin: 14px 0 8px;
}

.inline-form input {
    flex: 1;
}

.cat-edit {
    display: flex;
    gap: 8px;
    width: 100%;
}

.cat-edit input {
    flex: 1;
}

.settings-form {
    width: min(480px, 100%);
}

.settings-form input {
    margin-bottom: 8px;
}

.hint {
    margin-bottom: 12px;
}

.empty-note {
    padding: 8px 0 2px;
}

.hidden {
    display: none !important;
}

.modal {
    position: fixed;
    inset: 0;
    background: rgba(28, 25, 21, 0.4);
    display: grid;
    place-items: center;
    padding: 20px;
}

.modal-card {
    width: min(420px, 100%);
    padding: 22px;
}

.modal-card p {
    color: var(--muted);
}

@media (max-width: 1099px) {
    .app {
        grid-template-columns: 1fr;
    }

    .sidebar {
        position: sticky;
        top: 0;
        z-index: 20;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 16px;
        border-right: 0;
        border-bottom: 1px solid var(--line);
        padding: 12px 20px;
        background: rgba(255, 253, 248, 0.92);
        backdrop-filter: blur(10px);
    }

    .brand {
        margin-bottom: 0;
        flex-shrink: 0;
    }

    .nav {
        flex-direction: row;
        justify-content: flex-end;
        flex-wrap: wrap;
    }

    .nav-btn {
        min-height: 40px;
    }

    .stat-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .chart-grid,
    .cat-grid {
        grid-template-columns: 1fr 1fr;
    }
}

@media (max-width: 899px) {
    .chart-grid,
    .donut-wrap {
        grid-template-columns: 1fr;
    }

    #donut-chart {
        width: min(240px, 100%);
        margin-inline: auto;
    }
}

@media (max-width: 719px) {
    .sidebar {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
        padding: 12px 14px 10px;
    }

    .nav {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .nav-btn {
        text-align: center;
        min-height: 44px;
        padding: 10px 8px;
    }

    .content {
        padding: 16px 14px 36px;
    }

    .page-head {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
    }

    .page-head h2 {
        font-size: 26px;
    }

    .page-head .btn {
        width: 100%;
        min-height: 44px;
    }

    .stat-grid,
    .cat-grid,
    .form-grid {
        grid-template-columns: 1fr;
    }

    .span-2 {
        grid-column: auto;
    }

    .panel,
    .stat-card {
        padding: 14px;
    }

    .panel-head {
        flex-direction: column;
        align-items: flex-start;
    }

    .filters {
        display: grid;
        grid-template-columns: 1fr;
    }

    input,
    select {
        font-size: 16px;
        min-height: 44px;
    }

    .form-actions,
    .inline-form {
        flex-direction: column;
    }

    .form-actions .btn,
    .inline-form .btn {
        width: 100%;
        min-height: 44px;
    }

    .tx-item,
    .cat-item {
        flex-direction: column;
        align-items: stretch;
    }

    .row-actions {
        display: grid;
        grid-template-columns: 1fr 1fr;
    }

    .tx-amount {
        grid-column: 1 / -1;
        font-size: 18px;
    }

    .btn.small {
        min-height: 40px;
    }

    .cat-edit {
        flex-wrap: wrap;
    }

    .cat-edit input {
        flex: 1 1 100%;
    }

    .cat-name {
        margin-bottom: 0;
    }
}

@media (min-width: 1600px) {
    .app {
        grid-template-columns: 280px minmax(0, 1fr);
    }

    .page-head h2 {
        font-size: 40px;
    }

    .stat-grid,
    .chart-grid,
    .cat-grid {
        gap: 22px;
    }
}

@media (min-width: 2200px) {
    .app {
        max-width: 2000px;
        margin-inline: auto;
        border-inline: 1px solid var(--line);
    }
}
