:root {
    --bg: #f4f6f8;
    --card: #ffffff;
    --ink: #1f2933;
    --muted: #6b7280;
    --line: #e5e7eb;
    --primary: #2563eb;
    --primary-d: #1d4ed8;
    --danger: #dc2626;
    --ok: #16a34a;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--ink);
    line-height: 1.45;
}

a { color: var(--primary); }

/* ---------- Top bar ---------- */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
    background: var(--card);
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 10;
    flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 1.1rem; text-decoration: none; color: var(--ink); }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Layout ---------- */
.container { max-width: 1000px; margin: 0 auto; padding: 16px; }
.container.narrow { max-width: 640px; }
h2 { margin-top: 0; }

.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
    margin-bottom: 16px;
}

/* ---------- Buttons ---------- */
.btn {
    display: inline-block;
    padding: 9px 14px;
    border-radius: 9px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
    text-decoration: none;
    font-size: .92rem;
    cursor: pointer;
    line-height: 1;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-d); }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; padding: 12px; font-size: 1rem; }
.btn.tiny { padding: 5px 9px; font-size: .8rem; }
.btn.danger { color: var(--danger); border-color: #f3c2c2; }
.btn.danger:hover { background: #fee2e2; }

.link-danger { background: none; border: none; color: var(--danger); cursor: pointer; font-size: 1rem; }
.inline { display: inline; margin: 0; }

/* ---------- Alerts ---------- */
.alert { padding: 10px 14px; border-radius: 9px; margin-bottom: 14px; font-size: .92rem; }
.alert.ok { background: #dcfce7; color: #166534; }
.alert.error { background: #fee2e2; color: #991b1b; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ---------- Forms ---------- */
.form label, .login-card label { display: block; margin-bottom: 14px; font-weight: 600; font-size: .9rem; }
input, select, textarea {
    width: 100%;
    margin-top: 5px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 9px;
    font: inherit;
    background: #fff;
    color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); border-color: var(--primary); }
.row { display: flex; gap: 14px; }
.row > label { flex: 1; }
.actions { display: flex; gap: 10px; margin-top: 6px; }

/* ---------- Drag & drop uploader ---------- */
.field { margin-bottom: 14px; }
.field-label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 5px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

.dropzone {
    display: block;
    border: 2px dashed var(--line);
    border-radius: var(--radius);
    background: #fafbfc;
    padding: 22px 16px;
    text-align: center;
    cursor: pointer;
    transition: border-color .15s, background .15s;
    margin: 0;
}
.dropzone:hover { border-color: var(--primary); }
.dropzone.is-dragover { border-color: var(--primary); background: #eef4ff; }
.dropzone-inner { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.dropzone-icon { font-size: 1.7rem; }
.dropzone-text { font-size: .92rem; font-weight: 400; }
.link-like { color: var(--primary); text-decoration: underline; }

.file-preview { list-style: none; padding: 0; margin: 10px 0 0; }
.file-preview li { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 9px; margin-bottom: 6px; background: #fff; }
.file-preview .fp-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-preview .fp-size { font-size: .82rem; white-space: nowrap; }

.existing-files { margin-bottom: 14px; }
.file-list { list-style: none; padding: 0; margin: 6px 0 0; }
.file-list li { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--line); }

/* ---------- Login ---------- */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 20px; }
.login-card { background: var(--card); padding: 28px; border-radius: var(--radius); box-shadow: var(--shadow); width: 100%; max-width: 360px; }
.login-card h1 { margin: 0 0 4px; font-size: 1.4rem; }
.login-card p { margin-top: 0; }

/* ---------- Dashboard ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; text-align: center; }
.stat-label { display: block; font-size: .78rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat-value { display: block; font-size: 1.25rem; font-weight: 700; margin-top: 4px; }

.breakdown summary { cursor: pointer; font-weight: 600; }
.cat-bars { list-style: none; padding: 0; margin: 12px 0 0; }
.cat-bars li { display: grid; grid-template-columns: 1fr 2fr auto; gap: 10px; align-items: center; padding: 5px 0; font-size: .9rem; }
.bar { background: var(--line); border-radius: 6px; height: 10px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--primary); }
.cat-amt { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Manage categories ---------- */
.add-cat { display: flex; gap: 10px; align-items: center; }
.add-cat input { margin-top: 0; flex: 1; }
.cat-manage { list-style: none; padding: 0; margin: 0; }
.cat-manage li { display: flex; align-items: center; gap: 12px; padding: 11px 4px; border-bottom: 1px solid var(--line); }
.cat-manage li:last-child { border-bottom: none; }
.cat-manage-name { flex: 1; font-weight: 600; }

/* ---------- Filters ---------- */
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.filters input, .filters select { width: auto; margin-top: 0; flex: 1 1 160px; }
.filters .date-field { display: flex; align-items: center; gap: 6px; font-size: .85rem; color: var(--muted); margin: 0; font-weight: 600; }
.filters .date-field input { flex: 0 1 150px; }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; }
table.expenses { width: 100%; border-collapse: collapse; background: var(--card); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
table.expenses th, table.expenses td { padding: 11px 12px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; }
table.expenses th { background: #f9fafb; font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
table.expenses .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tag { display: inline-block; background: #eef2ff; color: #3730a3; padding: 2px 9px; border-radius: 999px; font-size: .78rem; }
.file-chip { text-decoration: none; margin-right: 2px; font-size: 1.05rem; }
.row-actions { white-space: nowrap; display: flex; gap: 6px; }
.empty { text-align: center; color: var(--muted); }

/* ---------- Mobile: turn table into cards ---------- */
@media (max-width: 680px) {
    .stats { grid-template-columns: 1fr; }
    .stat { display: flex; justify-content: space-between; align-items: center; text-align: left; }
    .stat-value { margin-top: 0; }
    .row { flex-direction: column; gap: 0; }

    table.expenses thead { display: none; }
    table.expenses, table.expenses tbody, table.expenses tr, table.expenses td { display: block; width: 100%; }
    table.expenses tr { margin-bottom: 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); overflow: hidden; }
    table.expenses td { border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; gap: 12px; text-align: right; }
    table.expenses td:last-child { border-bottom: none; }
    table.expenses td::before { content: attr(data-label); font-weight: 600; color: var(--muted); text-align: left; }
    table.expenses .num { text-align: right; }
    .row-actions { justify-content: flex-end; }
}
