:root {
    --bg: #1e242c;
    --bg-sidebar: #171c22;
    --surface: #272e38;
    --surface-2: #2e3640;
    --hover: #343c48;
    --border: #3a4350;
    --border-strong: #4d5868;
    --text: #eef0f3;
    --muted: #9aa3ae;
    --muted-2: #7f8894;
    --accent: #6d8fd4;
    --accent-fill: #4d6cb0;
    --accent-hover: #5b7bc0;
    --income: #73c197;
    --income-bg: #2f4a3b;
    --expense: #d49191;
    --expense-bg: #5a3a3a;
    --warning: #d4a574;
    --error: #d08989;
    --ok: #73c197;
    --nav-h: 64px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --max: 480px;
    --tap: 48px;
}

* { box-sizing: border-box; }
html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
}
html { height: 100%; }
body {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    min-height: 100dvh;
    -webkit-font-smoothing: antialiased;
}
body.is-guest { padding: var(--safe-top) 16px max(16px, var(--safe-bottom)); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

.app, .guest-shell, .app-root { max-width: var(--max); margin: 0 auto; }
.guest-shell { padding-bottom: 24px; }
.top { padding: 12px 0 8px; }
.brand { margin: 0; font-weight: 700; font-size: 1.05rem; }
h1 { font-size: 1.55rem; margin: 8px 0 12px; font-weight: 700; }
h2 { font-size: 0.95rem; margin: 0 0 10px; font-weight: 650; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.hero p { color: var(--muted); line-height: 1.45; }

.banner {
    padding: 12px 14px;
    border-radius: 12px;
    margin: 12px 0;
    line-height: 1.4;
}
.banner.error { background: var(--expense-bg); color: #f3d4d4; }
.banner.warn { background: #4a3a28; color: #f3e0c8; }
.banner.ok { background: var(--income-bg); color: #d4f0e0; }

.btn {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: var(--tap);
    padding: 12px 16px;
    border-radius: 12px;
    text-decoration: none;
    font-size: 1rem;
    font-weight: 600;
    border: 0;
    width: 100%;
    cursor: pointer;
    gap: 8px;
}
.btn.primary { background: var(--accent-fill); color: #fff; }
.btn.primary:active { background: var(--accent-hover); }
.btn.ghost {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
}
.btn.danger { background: var(--expense-bg); color: #f3d4d4; }
.btn.income { background: var(--income-bg); color: var(--income); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.icon {
    width: var(--tap);
    min-width: var(--tap);
    padding: 0;
}
.bottom-actions {
    position: sticky;
    bottom: 0;
    padding: 12px 0 calc(12px + var(--safe-bottom));
    background: var(--bg);
}

/* App chrome */
.app-root {
    min-height: 100dvh;
    padding: calc(12px + var(--safe-top)) 16px calc(var(--nav-h) + 12px + var(--safe-bottom));
    position: relative;
}
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: var(--tap);
    margin-bottom: 8px;
}
.app-header h1 {
    margin: 0;
    font-size: 1.25rem;
}
.cloud-dot {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: var(--tap);
    padding: 0 10px;
    border-radius: 999px;
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--muted);
    font-size: 0.85rem;
    cursor: pointer;
}
.cloud-dot .dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--ok);
}
.cloud-dot.busy .dot { background: var(--warning); }
.cloud-dot.off .dot { background: var(--error); }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 16px;
    margin: 0 0 12px;
}
.kpi-row {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
    margin-bottom: 12px;
}
.kpi {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 12px 8px;
    text-align: center;
    min-height: 76px;
}
.kpi .lbl { color: var(--muted); font-size: 0.75rem; margin-bottom: 6px; }
.kpi .val { font-size: 0.92rem; font-weight: 700; letter-spacing: -0.02em; word-break: break-word; }
.kpi.income .val { color: var(--income); }
.kpi.expense .val { color: var(--expense); }

.tx-row {
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: var(--tap);
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    text-decoration: none;
    color: inherit;
}
.tx-row:last-child { border-bottom: 0; }
.tx-main { flex: 1; min-width: 0; }
.tx-cat { font-weight: 600; }
.tx-sub { color: var(--muted); font-size: 0.85rem; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-amt { font-weight: 700; white-space: nowrap; }
.tx-amt.income { color: var(--income); }
.tx-amt.expense { color: var(--expense); }

.nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: grid;
    grid-template-columns: 1fr 1fr var(--tap) 1fr 1fr;
    gap: 0;
    max-width: var(--max);
    margin: 0 auto;
    background: var(--bg-sidebar);
    border-top: 1px solid var(--border);
    padding: 6px 8px calc(6px + var(--safe-bottom));
    z-index: 20;
}
.nav button {
    background: none;
    border: 0;
    color: var(--muted);
    min-height: var(--tap);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    font-size: 0.7rem;
    font-weight: 600;
    cursor: pointer;
    padding: 0 2px;
}
.nav button.active { color: var(--text); }
.nav .fab {
    width: var(--tap);
    height: var(--tap);
    border-radius: 50%;
    background: var(--accent-fill);
    color: #fff;
    font-size: 1.6rem;
    line-height: 1;
    margin: 0 auto;
    font-weight: 500;
}
.nav .fab.active { background: var(--accent-hover); }

.field { margin: 0 0 14px; }
.field label {
    display: block;
    font-size: 0.85rem;
    color: var(--muted);
    margin-bottom: 6px;
}
.field input, .field select, .field textarea {
    width: 100%;
    min-height: var(--tap);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 10px 12px;
}
.field textarea { min-height: 88px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}
.amount-input {
    font-size: 2rem;
    font-weight: 700;
    text-align: center;
    letter-spacing: -0.03em;
    min-height: 64px !important;
}
.seg {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}
.seg button {
    min-height: var(--tap);
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    cursor: pointer;
    font-weight: 650;
}
.seg button.on-income { background: var(--income-bg); color: var(--income); border-color: transparent; }
.seg button.on-expense { background: var(--expense-bg); color: var(--expense); border-color: transparent; }

.chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}
.chip {
    min-height: var(--tap);
    padding: 0 14px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-weight: 600;
}
.chip.on { background: var(--accent-fill); border-color: transparent; color: #fff; }
.chip .star {
    width: var(--tap);
    min-width: var(--tap);
    height: var(--tap);
    margin: 0 -10px 0 -8px;
    display: grid;
    place-items: center;
    background: none;
    border: 0;
    color: var(--warning);
    cursor: pointer;
    font-size: 1.15rem;
}

.sheet-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 30;
}
.sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    max-width: var(--max);
    margin: 0 auto;
    background: var(--surface);
    border-radius: 18px 18px 0 0;
    padding: 12px 16px calc(16px + var(--safe-bottom));
    z-index: 31;
    max-height: 88dvh;
    overflow: auto;
}
.sheet h2 { color: var(--text); text-transform: none; font-size: 1.1rem; letter-spacing: 0; }
.sheet-handle {
    width: 40px;
    height: 4px;
    background: var(--border-strong);
    border-radius: 4px;
    margin: 4px auto 12px;
}

.cat-row {
    display: grid;
    grid-template-columns: var(--tap) 1fr auto;
    align-items: center;
    min-height: var(--tap);
    gap: 4px;
}
.cat-row button.star-btn {
    width: var(--tap);
    height: var(--tap);
    background: none;
    border: 0;
    color: var(--muted-2);
    cursor: pointer;
    font-size: 1.2rem;
}
.cat-row button.star-btn.on { color: var(--warning); }
.search-bar {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
}
.search-bar input { flex: 1; }
.sticky-save {
    position: sticky;
    bottom: 0;
    padding: 10px 0 calc(8px + var(--safe-bottom));
    background: linear-gradient(transparent, var(--bg) 28%);
}

.toast {
    position: fixed;
    left: 16px;
    right: 16px;
    bottom: calc(var(--nav-h) + 12px + var(--safe-bottom));
    max-width: calc(var(--max) - 32px);
    margin: 0 auto;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 14px;
    z-index: 40;
    text-align: center;
    font-weight: 600;
}
.muted { color: var(--muted); }
.center { text-align: center; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.row-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.chart-wrap { height: 140px; }
.bar-row { display: flex; align-items: center; gap: 8px; margin: 6px 0; font-size: 0.9rem; }
.bar-row .n { flex: 0 0 92px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { flex: 1; height: 10px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--expense); border-radius: 99px; }
.insight { padding: 8px 0; color: var(--muted); font-size: 0.92rem; line-height: 1.4; border-bottom: 1px solid var(--border); }
.insight:last-child { border-bottom: 0; }

.skeleton-block, .skeleton-card {
    background: var(--surface);
    border-radius: 12px;
    height: 22px;
    margin: 10px 0;
    animation: pulse 1.2s ease-in-out infinite;
}
.skeleton-block.short { width: 40%; }
.skeleton-card { height: 88px; }
@keyframes pulse { 50% { opacity: 0.55; } }

.empty { color: var(--muted); padding: 24px 8px; text-align: center; line-height: 1.45; }
.page-title-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.filter-btn { width: auto; min-width: var(--tap); padding: 0 14px; }

@media (min-width: 720px) {
    .app-root, .nav, .sheet, .toast, .guest-shell { max-width: var(--max); }
}
