:root {
    --bg: #f1f5f9;
    --card: #ffffff;
    --text: #0f172a;
    --muted: #64748b;
    --border: #e2e8f0;
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --green: #22c55e;
    --red: #ef4444;
    --input-bg: #ffffff;
    --btn-secondary: #e2e8f0;
    --alert-error-bg: #fef2f2;
    --alert-error-text: #991b1b;
    --alert-error-border: #fecaca;
    --alert-ok-bg: #f0fdf4;
    --alert-ok-text: #166534;
    --alert-ok-border: #bbf7d0;
    --voice-bg: linear-gradient(135deg, #eef2ff, #ffffff);
    --shadow: 0 1px 3px rgba(0, 0, 0, .08), 0 1px 2px rgba(0, 0, 0, .04);
}

[data-theme="dark"] {
    --bg: #0f172a;
    --card: #1e293b;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --border: #334155;
    --primary: #818cf8;
    --primary-dark: #6366f1;
    --green: #4ade80;
    --red: #f87171;
    --input-bg: #0f172a;
    --btn-secondary: #334155;
    --alert-error-bg: #450a0a;
    --alert-error-text: #fecaca;
    --alert-error-border: #7f1d1d;
    --alert-ok-bg: #052e16;
    --alert-ok-text: #bbf7d0;
    --alert-ok-border: #166534;
    --voice-bg: linear-gradient(135deg, #1e293b, #0f172a);
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    line-height: 1.5;
}

/* ===== Topbar & Nav ===== */
.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    background: var(--card);
    border-bottom: 1px solid var(--border);
    padding: .65rem 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .5rem;
}

.brand { font-weight: 700; font-size: 1.1rem; }

.nav {
    display: flex;
    gap: .35rem;
    flex-wrap: wrap;
}

.nav-link {
    text-decoration: none;
    color: var(--muted);
    font-size: .85rem;
    padding: .35rem .6rem;
    border-radius: .5rem;
    transition: background .15s, color .15s;
}

.nav-link:hover { background: var(--bg); color: var(--text); }
.nav-link.active { background: var(--primary); color: #fff; }

.nav-user {
    font-size: .8rem;
    font-weight: 600;
    color: var(--primary);
    padding: .35rem .5rem;
}

.theme-toggle {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: .5rem;
    padding: .3rem .55rem;
    font-size: .9rem;
    cursor: pointer;
    color: var(--text);
    line-height: 1;
    transition: background .15s;
}

.theme-toggle:hover { background: var(--bg); }

.theme-toggle-fixed {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 20;
    background: var(--card);
    box-shadow: var(--shadow);
}

.container {
    max-width: 860px;
    margin: 0 auto;
    padding: 1rem;
}

/* ===== Cards ===== */
.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: .9rem;
    padding: 1rem 1.1rem;
    box-shadow: var(--shadow);
    margin-bottom: 1rem;
}

.card-title { font-size: 1rem; margin: 0 0 .75rem; }
.page-title { font-size: 1.25rem; margin: 0 0 1rem; }
.muted { color: var(--muted); }

/* ===== Summary ===== */
.summary-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
    gap: .75rem;
    margin-bottom: 1rem;
}

.summary-card { display: flex; flex-direction: column; gap: .15rem; }
.summary-label { font-size: .8rem; color: var(--muted); }
.summary-value { font-size: 1.25rem; font-weight: 700; }
.summary-sub { font-size: .75rem; color: var(--muted); }

.green { color: var(--green); }
.red { color: var(--red); }

/* ===== Transactions list ===== */
.tx-list { display: flex; flex-direction: column; }

.tx-item {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: .6rem 0;
    border-bottom: 1px solid var(--border);
}

.tx-item:last-child { border-bottom: none; }

.tx-icon { font-size: 1.3rem; }

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

.tx-desc {
    font-weight: 600;
    font-size: .9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.tx-meta { font-size: .75rem; color: var(--muted); }

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

.tx-edit { text-decoration: none; font-size: 1rem; }

/* ===== Alerts ===== */
.alert {
    border-radius: .6rem;
    padding: .6rem .8rem;
    font-size: .85rem;
    margin-bottom: .75rem;
}

.alert-error { background: var(--alert-error-bg); color: var(--alert-error-text); border: 1px solid var(--alert-error-border); }
.alert-ok { background: var(--alert-ok-bg); color: var(--alert-ok-text); border: 1px solid var(--alert-ok-border); }

/* ===== Forms ===== */
.field {
    display: flex;
    flex-direction: column;
    gap: .3rem;
    margin-bottom: .85rem;
}

.field > span { font-size: .8rem; color: var(--muted); font-weight: 600; }

input[type=text], input[type=password], input[type=date], input[type=month],
select, textarea {
    width: 100%;
    padding: .6rem .7rem;
    border: 1px solid var(--border);
    border-radius: .6rem;
    font-size: .95rem;
    background: var(--input-bg);
    color: var(--text);
    font-family: inherit;
}

input:focus, select:focus, textarea:focus {
    outline: 2px solid var(--primary);
    outline-offset: 0;
    border-color: transparent;
}

textarea { resize: vertical; }

.btn {
    display: inline-block;
    border: none;
    border-radius: .6rem;
    padding: .6rem 1rem;
    font-size: .95rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: filter .15s;
}

.btn:hover { filter: brightness(1.05); }
.btn-block { width: 100%; }

.btn-primary { background: var(--primary); color: #fff; }
.btn-secondary { background: var(--btn-secondary); color: var(--text); }
.btn-danger { background: var(--red); color: #fff; }
.btn-mic { background: var(--card); color: var(--primary); border: 2px solid var(--primary); font-size: 1rem; }

.btn-mic.recording {
    background: var(--red);
    color: #fff;
    border-color: var(--red);
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, .5); }
    50% { box-shadow: 0 0 0 8px rgba(239, 68, 68, 0); }
}

.type-toggle {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: .5rem;
}

.type-option { position: relative; }
.type-option input { position: absolute; opacity: 0; }

.type-btn {
    display: block;
    text-align: center;
    padding: .7rem;
    border-radius: .6rem;
    border: 2px solid var(--border);
    font-weight: 600;
    font-size: .9rem;
    cursor: pointer;
    background: var(--input-bg);
    transition: all .15s;
}

.expense-btn:hover { border-color: var(--red); color: var(--red); }
.income-btn:hover { border-color: var(--green); color: var(--green); }

.type-option input:checked + .expense-btn { background: var(--red); border-color: var(--red); color: #fff; }
.type-option input:checked + .income-btn { background: var(--green); border-color: var(--green); color: #fff; }

/* ===== Voice box ===== */
.voice-box {
    background: var(--voice-bg);
    border: 1px dashed var(--primary);
    display: flex;
    flex-direction: column;
    gap: .6rem;
}

.voice-status { font-size: .85rem; color: var(--primary); font-weight: 600; }
.voice-text { width: 100%; }

.delete-form { margin-top: 1rem; }

/* ===== Filter form ===== */
.filter-form {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: .6rem;
    align-items: end;
}

.filter-form .btn { margin-bottom: .85rem; }

/* ===== Category bars ===== */
.cat-bars { display: flex; flex-direction: column; gap: .6rem; }

.cat-bar-row {
    display: grid;
    grid-template-columns: 130px 1fr 90px;
    align-items: center;
    gap: .6rem;
}

.cat-bar-label { font-size: .8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cat-bar-label small { color: var(--muted); }
.cat-bar-track { background: var(--bg); border-radius: 999px; height: .7rem; overflow: hidden; }
.cat-bar-fill { height: 100%; border-radius: 999px; }
.cat-bar-value { font-size: .8rem; font-weight: 600; text-align: right; white-space: nowrap; }

/* ===== Login ===== */
.login-body {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.login-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 1rem;
    box-shadow: var(--shadow);
    padding: 2rem;
    width: 100%;
    max-width: 360px;
    text-align: center;
}

.login-logo { font-size: 3rem; }

/* ===== FAB ===== */
.fab {
    position: fixed;
    bottom: 1.25rem;
    right: 1.25rem;
    width: 3.5rem;
    height: 3.5rem;
    border-radius: 999px;
    background: var(--primary);
    color: #fff;
    font-size: 1.6rem;
    display: flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    box-shadow: 0 4px 12px rgba(99, 102, 241, .45);
    transition: transform .15s;
}

.fab:hover { transform: scale(1.08); }

/* ===== Chart ===== */
.chart-card canvas { max-height: 280px; }

@media (max-width: 520px) {
    .topbar { flex-direction: column; align-items: flex-start; }
    .tx-amount { font-size: .8rem; }
}
