:root {
    --bg: #0b0f18;
    --bg-glow: radial-gradient(1200px 600px at 15% -10%, rgba(79,140,255,0.10), transparent 60%),
               radial-gradient(900px 500px at 100% 0%, rgba(57,135,229,0.08), transparent 55%);
    --panel: #161d2e;
    --panel-2: #1b2436;
    --border: #29324a;
    --border-soft: #232c40;
    --text: #e9ecf3;
    --text-dim: #9aa4bc;
    --text-faint: #6c7690;
    --accent: #4f8cff;
    --accent-2: #7cb2ff;
    --accent-dim: #2c5aa8;
    --accent-wash: rgba(79,140,255,0.12);
    /* Diverging pair for polarity (inflow/outflow, positive/negative) —
       shared by stat tiles, transaction amounts, and the cash-flow chart. */
    --positive: #3987e5;
    --negative: #e66767;
    --diverging-mid: #383835;
    --radius: 10px;
    --radius-sm: 7px;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.24);
    --shadow-md: 0 8px 24px rgba(0,0,0,0.28);
    --shadow-glow: 0 0 0 1px rgba(79,140,255,0.16), 0 8px 28px rgba(79,140,255,0.10);
}

* { box-sizing: border-box; }

::selection { background: var(--accent-dim); color: #fff; }

html { scrollbar-color: var(--border) transparent; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg-glow), var(--bg);
    background-attachment: fixed;
    color: var(--text);
    font-size: 15px;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

a { color: var(--accent-2); text-decoration: none; transition: color 0.15s ease; }
a:hover { color: var(--accent-2); text-decoration: underline; }

/* ---------- Topbar ---------- */

header.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 24px;
    background: rgba(22, 29, 46, 0.86);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border-soft);
}

header.topbar .brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    font-size: 15px;
    letter-spacing: 0.02em;
    color: var(--text);
}

header.topbar .brand .mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    background: linear-gradient(155deg, var(--accent) 0%, #2f66c9 100%);
    box-shadow: 0 2px 8px rgba(79,140,255,0.35);
    color: #fff;
    font-size: 13px;
    font-weight: 800;
}

header.topbar nav {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-wrap: wrap;
}

header.topbar nav a {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-left: 4px;
    padding: 7px 12px;
    border-radius: 999px;
    color: var(--text-dim);
    font-size: 13.5px;
    font-weight: 500;
}

header.topbar nav a svg { opacity: 0.85; flex-shrink: 0; }

header.topbar nav a:hover {
    color: var(--text);
    background: rgba(255,255,255,0.05);
    text-decoration: none;
}

header.topbar nav a.active {
    color: #fff;
    background: var(--accent-wash);
}

header.topbar nav a.active svg { opacity: 1; }

header.topbar nav a.logout-link {
    margin-left: 12px;
    padding-left: 14px;
    border-left: 1px solid var(--border-soft);
    border-radius: 0;
    color: var(--text-faint);
}
header.topbar nav a.logout-link:hover { color: var(--negative); background: none; }

/* ---------- Layout ---------- */

main {
    max-width: 1100px;
    margin: 0 auto;
    padding: 32px 24px 72px;
}

h1 {
    font-size: 23px;
    font-weight: 700;
    letter-spacing: -0.01em;
    margin: 0 0 20px;
}

h2 {
    font-size: 16px;
    font-weight: 600;
    margin: 30px 0 12px;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 8px;
}

.panel {
    background: linear-gradient(180deg, var(--panel-2) 0%, var(--panel) 100%);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 22px;
    margin-bottom: 20px;
    box-shadow: var(--shadow-sm);
}

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

/* ---------- Stat tiles ---------- */

.stat {
    position: relative;
    background: linear-gradient(180deg, var(--panel-2) 0%, var(--panel) 100%);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px 20px;
    overflow: hidden;
    box-shadow: var(--shadow-sm);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.stat::before {
    content: "";
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 2px;
    background: linear-gradient(90deg, var(--accent), transparent);
    opacity: 0.7;
}
.stat:hover { transform: translateY(-1px); box-shadow: var(--shadow-md); }

.stat .label {
    color: var(--text-dim);
    font-size: 12.5px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 8px;
}
.stat .value {
    font-size: 25px;
    font-weight: 700;
    letter-spacing: -0.01em;
    font-variant-numeric: tabular-nums;
}
.stat .value.positive { color: var(--positive); }
.stat .value.negative { color: var(--negative); }

/* ---------- Tables ---------- */

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 12px; border-bottom: 1px solid var(--border-soft); font-size: 14px; }
th {
    color: var(--text-faint);
    font-weight: 700;
    font-size: 11.5px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    background: var(--panel);
}
/* Opt-in only — for long, scrolled-through lists (Transactions, Square
   Sales). On a short table this makes the header float over the first
   body row instead of scrolling normally past it. */
table.sticky-thead th {
    position: sticky;
    top: 57px;
}
tbody tr { transition: background 0.1s ease; }
tbody tr:hover td { background: rgba(255,255,255,0.025); }
tbody tr:last-child td { border-bottom: none; }

.amount { font-variant-numeric: tabular-nums; font-weight: 600; }
.amount.positive { color: var(--positive); }
.amount.negative { color: var(--negative); }

/* ---------- Cash-flow chart ---------- */

.cashflow-chart { display: flex; flex-direction: column; gap: 3px; }
.cashflow-row { display: grid; grid-template-columns: 56px 1fr 90px; align-items: center; gap: 10px; padding: 3px 0; border-radius: 6px; }
.cashflow-row:hover { background: rgba(255,255,255,0.025); }
.cashflow-row .month-label { color: var(--text-dim); font-size: 12px; text-align: right; font-variant-numeric: tabular-nums; }
.cashflow-track { position: relative; height: 20px; background: transparent; }
.cashflow-track .baseline { position: absolute; left: 50%; top: 0; bottom: 0; width: 1px; background: var(--diverging-mid); }
.cashflow-track .bar { position: absolute; top: 2px; height: 16px; transition: width 0.25s ease; }
.cashflow-track .bar.inflow { left: calc(50% + 1px); background: var(--positive); border-radius: 0 4px 4px 0; }
.cashflow-track .bar.outflow { right: calc(50% + 1px); background: var(--negative); border-radius: 4px 0 0 4px; }
.cashflow-row .net-label { font-size: 12px; font-variant-numeric: tabular-nums; text-align: right; }
.cashflow-legend { display: flex; gap: 18px; margin-bottom: 16px; font-size: 12.5px; color: var(--text-dim); }
.cashflow-legend .swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: middle; }

/* ---------- Forms ---------- */

form.stacked label {
    display: block;
    margin: 16px 0 6px;
    color: var(--text-dim);
    font-size: 13px;
    font-weight: 600;
}
form.stacked label:first-of-type { margin-top: 0; }

input[type=text], input[type=password], input[type=file], input[type=date], input[type=number], select, textarea {
    width: 100%;
    padding: 10px 12px;
    background: #0d1220;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 14px;
    font-family: inherit;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input:hover, select:hover, textarea:hover { border-color: #37426067; }
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-wash);
}
input[type=file] { padding: 9px 12px; cursor: pointer; }

.file-drop {
    border: 1.5px dashed var(--border);
    border-radius: var(--radius-sm);
    padding: 6px;
    background: rgba(255,255,255,0.015);
    transition: border-color 0.15s ease, background 0.15s ease;
}
.file-drop:hover { border-color: var(--accent-dim); background: var(--accent-wash); }
.file-drop input[type=file] { border: none; background: transparent; padding: 10px; }
input::placeholder, textarea::placeholder { color: var(--text-faint); }

label input[type=checkbox] { accent-color: var(--accent); }

/* ---------- Buttons ---------- */

button, .btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: linear-gradient(155deg, var(--accent) 0%, #2f66c9 100%);
    color: #fff;
    border: 1px solid transparent;
    padding: 10px 18px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    margin-top: 16px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.2);
    transition: transform 0.1s ease, box-shadow 0.15s ease, background 0.15s ease;
}
button:hover, .btn:hover {
    box-shadow: 0 4px 14px rgba(79,140,255,0.35);
    transform: translateY(-1px);
    text-decoration: none;
    color: #fff;
}
button:active, .btn:active { transform: translateY(0); }
button:focus-visible, .btn:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }

button.secondary, .btn.secondary {
    background: rgba(255,255,255,0.03);
    border: 1px solid var(--border);
    color: var(--text);
    box-shadow: none;
}
button.secondary:hover, .btn.secondary:hover {
    background: rgba(255,255,255,0.06);
    box-shadow: none;
    border-color: #3a4560;
}

/* ---------- Badges ---------- */

.badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 10px 3px 8px;
    border-radius: 999px;
    font-size: 11.5px;
    font-weight: 600;
    text-transform: capitalize;
    background: rgba(255,255,255,0.07);
    color: var(--text-dim);
}
.badge::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
    opacity: 0.9;
}
.badge.parsed { background: rgba(57,135,229,0.15); color: var(--positive); }
.badge.failed { background: rgba(230,103,103,0.15); color: var(--negative); }

.view-toggle {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 20px;
    font-size: 13px;
}
.view-toggle a {
    padding: 5px 12px;
    border-radius: 999px;
    color: var(--text-dim);
    background: rgba(255,255,255,0.04);
    border: 1px solid var(--border);
}
.view-toggle a:hover { color: var(--text); text-decoration: none; background: rgba(255,255,255,0.07); }
.view-toggle a.active { color: #fff; background: var(--accent-wash); border-color: var(--accent-dim); }
.badge.uploaded, .badge.mapping, .badge.parsing { background: rgba(79,140,255,0.15); color: var(--accent-2); }

/* ---------- Flash messages ---------- */

.flash {
    display: flex;
    align-items: center;
    gap: 9px;
    padding: 11px 15px;
    border-radius: var(--radius-sm);
    margin-bottom: 18px;
    font-size: 14px;
    font-weight: 500;
    animation: flash-in 0.2s ease;
}
.flash::before { font-weight: 700; flex-shrink: 0; }
.flash.error { background: rgba(230,103,103,0.12); color: #ff9aab; border: 1px solid rgba(230,103,103,0.28); }
.flash.error::before { content: "\2715"; }
.flash.success { background: rgba(57,135,229,0.12); color: #9cc6f7; border: 1px solid rgba(57,135,229,0.28); }
.flash.success::before { content: "\2713"; }

@keyframes flash-in {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ---------- Empty states ---------- */

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 48px 20px;
    color: var(--text-faint);
}
.empty-state .icon {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--accent-wash);
    color: var(--accent-2);
    margin-bottom: 14px;
}
.empty-state p { margin: 0 0 4px; color: var(--text-dim); font-size: 14.5px; }
.empty-state .hint { font-size: 13px; color: var(--text-faint); }
.empty-state a { color: var(--accent-2); }

/* ---------- Login ---------- */

.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}
.login-box {
    width: 100%;
    max-width: 360px;
    box-shadow: var(--shadow-glow), var(--shadow-md);
}
.login-box .brand-lockup {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 4px;
}
.login-box .brand-lockup .mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 11px;
    background: linear-gradient(155deg, var(--accent) 0%, #2f66c9 100%);
    box-shadow: 0 4px 14px rgba(79,140,255,0.4);
    color: #fff;
    font-size: 17px;
    font-weight: 800;
    flex-shrink: 0;
}
.login-box h1 { font-size: 21px; margin: 0; }

/* ---------- Misc utilities ---------- */

.text-dim { color: var(--text-dim); }
.text-faint { color: var(--text-faint); }
.mt-0 { margin-top: 0; }

footer.app-footer {
    max-width: 1100px;
    margin: 0 auto;
    padding: 0 24px 40px;
    color: var(--text-faint);
    font-size: 12.5px;
}

/* ---------- Responsive ---------- */

@media (max-width: 720px) {
    header.topbar { flex-direction: column; align-items: flex-start; gap: 10px; padding: 14px 18px; }
    header.topbar nav { width: 100%; }
    header.topbar nav a.logout-link { margin-left: 0; padding-left: 12px; }
    main { padding: 22px 16px 56px; }
    th { position: static; }
    .cashflow-row { grid-template-columns: 44px 1fr 70px; gap: 6px; }
}
