:root {
    --bg: #0f1117;
    --bg-card: #181b24;
    --border: #262a36;
    --text: #e2e5ec;
    --text-muted: #8b90a0;
    --accent: #6366f1;
    --green: #34d399;
    --red: #f87171;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    min-height: 100vh;
    color-scheme: dark;
}

.topbar {
    display: flex;
    align-items: center;
    gap: 2rem;
    padding: 0.9rem 1.5rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg-card);
    position: sticky;
    top: 0;
    z-index: 20;
}

.brand {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--accent);
    letter-spacing: 0.03em;
}

.topbar nav { display: flex; gap: 1.2rem; }

.topbar a {
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.95rem;
}

.topbar a:hover { color: var(--text); }

.content { padding: 1.5rem; max-width: 1400px; margin: 0 auto; }

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 1.2rem;
}

.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.2rem 1.4rem;
}

.card h2 {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    margin-bottom: 0.9rem;
}

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

.widget-row {
    display: flex;
    gap: 2.5rem;
    margin-bottom: 0.8rem;
}

.metric { font-size: 1.6rem; font-weight: 700; }
.metric.green { color: var(--green); }
.metric.red { color: var(--red); }

.list { list-style: none; }
.list li {
    padding: 0.45rem 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.92rem;
}
.list li:last-child { border-bottom: none; }

.list a { color: var(--text); text-decoration: none; }
.list a:hover { color: var(--accent); }

table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { text-align: left; padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--border); }
th { color: var(--text-muted); font-weight: 500; font-size: 0.82rem; }

input, select, button {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 8px;
    height: 38px;
    padding: 0 0.75rem;
    font-family: inherit;
    font-size: 0.92rem;
    line-height: 1.2;
}

input::placeholder { color: var(--text-muted); opacity: 1; }

select {
    -webkit-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%238b90a0' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.7rem center;
    padding-right: 2.1rem;
}

input[type="date"] {
    -webkit-appearance: none;
    appearance: none;
}

button {
    background: var(--accent);
    border: 1px solid var(--accent);
    cursor: pointer;
    font-weight: 600;
}

button:hover { opacity: 0.9; }

.budget-form { display: flex; flex-direction: column; gap: 0.6rem; }

.chart { width: 100%; height: auto; }
.chart-label { fill: var(--text-muted); font-size: 11px; }

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

/* --- Budget Book --- */

.page-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.8rem;
    margin-bottom: 1.2rem;
}

.page-head h1 { font-size: 1.4rem; }

.year-switch { display: flex; gap: 0.4rem; }
.year-switch a {
    padding: 0.35rem 0.9rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.92rem;
}
.year-switch a.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 600;
}
.year-switch a:hover:not(.active) { color: var(--text); border-color: var(--text-muted); }

.back-link { color: var(--text-muted); text-decoration: none; font-size: 0.92rem; }
.back-link:hover { color: var(--text); }

.kpi-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.2rem;
    margin-bottom: 1.2rem;
}
.kpi { display: flex; flex-direction: column; gap: 0.3rem; }
.kpi-label {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
}

.budget-grid { margin-bottom: 1.2rem; }
.chart-card { display: flex; flex-direction: column; }
.chart-card .chart { flex: 1; min-height: 0; }
.bar-value-label { font-size: 12px; font-weight: 600; opacity: 0; pointer-events: none; transition: opacity 0.1s; }
.bar-value-label.pos { fill: var(--green); }
.bar-value-label.neg { fill: var(--red); }
.bar-group:hover .bar-value-label { opacity: 1; }

.bar-row {
    display: grid;
    grid-template-columns: 130px 1fr 90px;
    align-items: center;
    gap: 0.7rem;
    padding: 0.3rem 0;
    font-size: 0.88rem;
}
.bar-label { color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { background: var(--bg); border-radius: 4px; height: 14px; overflow: hidden; }
.bar-fill { background: var(--accent); height: 100%; border-radius: 4px; }
.bar-value { text-align: right; font-variant-numeric: tabular-nums; }

.form-card { margin-bottom: 1.2rem; position: relative; }
.budget-form-inline { display: flex; flex-wrap: wrap; gap: 0.8rem; align-items: center; }
.budget-form-inline input[name="amount"],
.budget-form-inline select[name="category"],
.budget-form-inline input[type="date"] { flex: 1 1 0; min-width: 140px; }
.budget-form-inline button { flex: 0 0 auto; padding: 0.5rem 2rem; }
.last-entry {
    position: absolute;
    right: 0.55rem;
    bottom: 0.2rem;
    font-size: 0.62rem;
    opacity: 0.55;
    white-space: nowrap;
}

.matrix-card { overflow: hidden; margin-bottom: 1.2rem; }
.matrix-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.6rem; }
.matrix-scroll { overflow-x: auto; }

.matrix { font-size: 0.84rem; white-space: nowrap; }
.matrix th, .matrix td { padding: 0.35rem 0.55rem; }
.matrix td.num, .matrix th.num { text-align: right; font-variant-numeric: tabular-nums; }
.matrix td.num a { color: inherit; text-decoration: none; border-bottom: 1px dotted var(--text-muted); }
.matrix td.num a:hover { color: var(--accent); border-bottom-color: var(--accent); }
.matrix .sticky {
    position: sticky;
    left: 0;
    background: var(--bg-card);
    z-index: 1;
    min-width: 180px;
}
.matrix .cur { background: rgba(99, 102, 241, 0.10); }
.matrix .col-total { font-weight: 600; border-left: 1px solid var(--border); }
.matrix .col-avg { color: var(--text-muted); }
.matrix td.neg { color: var(--red); }
.matrix td.pos { color: var(--green); }
.matrix .zero { color: var(--text-muted); opacity: 0.5; }

.section-row td {
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-size: 0.74rem;
    color: var(--text-muted);
    padding-top: 0.9rem;
    border-bottom: none;
}
.group-row { cursor: pointer; }
.group-row td { font-weight: 600; }
.group-row:hover td { background: rgba(255, 255, 255, 0.03); }
.group-row:hover td.sticky { background: var(--bg-card); }
.group-name::before {
    content: "▸";
    display: inline-block;
    margin-right: 0.4rem;
    color: var(--text-muted);
    transition: transform 0.15s;
}
.group-row.open .group-name::before { transform: rotate(90deg); }
.share { color: var(--text-muted); font-weight: 400; font-size: 0.78rem; margin-left: 0.4rem; }
.cat-row td.sticky { padding-left: 1.6rem; color: var(--text-muted); }
.cat-row.hidden { display: none; }
.total-row td { font-weight: 700; border-top: 1px solid var(--border); }
.total-row.grand td { border-top: 2px solid var(--border); }
.total-row.net td { font-size: 0.9rem; }
.matrix-hint { margin-top: 0.7rem; }
.matrix-hint a { color: var(--accent); text-decoration: none; }

button.ghost {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-muted);
    font-weight: 500;
}
button.ghost:hover { color: var(--text); border-color: var(--text-muted); opacity: 1; }
button.ghost.danger:hover { color: var(--red); border-color: var(--red); }
td button.ghost { height: 26px; padding: 0 0.5rem; font-size: 0.8rem; }

.filter-form { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-bottom: 0.9rem; align-items: center; }
.filter-info { margin-bottom: 0.7rem; }
.range-label { display: flex; align-items: center; gap: 0.4rem; font-size: 0.92rem; }

/* Login page */
.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
}
.login-card {
    display: flex;
    flex-direction: column;
    gap: 0.8rem;
    width: 100%;
    max-width: 360px;
    padding: 2rem 1.8rem;
}
.login-card .brand { font-size: 1.3rem; }
.login-card h2 { margin-bottom: 0; }
.login-error { color: var(--red); font-size: 0.88rem; }
.topbar form.logout { margin-left: auto; }
.topbar form.logout button {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-muted);
    height: 30px;
    padding: 0 0.8rem;
    font-size: 0.85rem;
}
.topbar form.logout button:hover { color: var(--text); border-color: var(--text-muted); opacity: 1; }
