/* BudgetBook application styles. Self-contained (CSP: default-src 'self'). */

:root {
    --bg: #0f1720;
    --surface: #17212e;
    --surface-2: #1e2a3a;
    --line: #26374a;
    --text: #e6edf3;
    --muted: #93a4b8;
    --accent: #3d8bfd;
    --accent-ink: #ffffff;
    --ok: #35c98a;
    --fail: #ff6b6b;
    --warn: #f0b429;
    --input-bg: #0e1620;
    --radius: 12px;
    --content-width: 1100px;
}

/* Light theme fallback for users/environments that prefer light. */
@media (prefers-color-scheme: light) {
    :root {
        --bg: #f3f5f8;
        --surface: #ffffff;
        --surface-2: #eef1f6;
        --line: #d7dee8;
        --text: #1a2330;
        --muted: #5c6b7e;
        --accent: #1f6feb;
        --accent-ink: #ffffff;
        --input-bg: #ffffff;
    }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

/* Accessibility: skip link + visible keyboard focus. */
.skip-link {
    position: absolute;
    left: -9999px;
    top: 0;
    background: var(--accent);
    color: var(--accent-ink);
    padding: 0.6rem 1rem;
    border-radius: 0 0 8px 0;
    z-index: 100;
}
.skip-link:focus { left: 0; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (prefers-reduced-motion: reduce) {
    * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

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

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

/* ---- Forms ---- */
.field { display: block; margin-bottom: 1rem; }
.field > span { display: block; margin-bottom: 0.3rem; font-size: 0.9rem; color: var(--muted); }
.field input, .field select, .field textarea {
    width: 100%;
    padding: 0.6rem 0.7rem;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: var(--input-bg);
    color: var(--text);
    font-size: 1rem;
    font-family: inherit;
}
.field input:focus, .field select:focus, .field textarea:focus {
    outline: 2px solid var(--accent);
    border-color: var(--accent);
}
.check { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 1.1rem; color: var(--muted); font-size: 0.95rem; }
.check input { width: 1rem; height: 1rem; }
.code-input { letter-spacing: 0.5em; text-align: center; font-size: 1.4rem; }

/* ---- Buttons ---- */
.btn {
    display: inline-block;
    padding: 0.6rem 1.1rem;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: transparent;
    color: var(--text);
    font-size: 0.95rem;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    font-family: inherit;
}
.btn:hover { border-color: var(--muted); }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn--primary:hover { filter: brightness(1.08); }
.btn--block { width: 100%; }
.btn--link { border: none; background: none; color: var(--accent); padding: 0.4rem; }
.btn--danger { color: var(--fail); border-color: var(--fail); }

/* ---- Alerts ---- */
.alert { padding: 0.75rem 1rem; border-radius: 8px; margin: 0 0 1.1rem; }
.alert ul { margin: 0; padding-left: 1.2rem; }
.alert--error { background: rgba(255,107,107,0.12); border: 1px solid var(--fail); }
.alert--warn { background: rgba(240,180,41,0.12); border: 1px solid var(--warn); }
.alert--ok { background: rgba(53,201,138,0.12); border: 1px solid var(--ok); }

/* ---- Auth screens ---- */
.auth-body { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.auth-shell { width: 100%; max-width: 420px; }
.auth-card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 2rem 1.75rem;
}
.auth-brand { display: flex; align-items: center; gap: 0.6rem; justify-content: center; margin-bottom: 1.5rem; }
.auth-brand h1 { margin: 0; font-size: 1.5rem; letter-spacing: -0.02em; }
.auth-logo {
    display: inline-grid; place-items: center;
    width: 2.2rem; height: 2.2rem;
    background: var(--accent); color: var(--accent-ink);
    border-radius: 10px; font-weight: 700;
}
.auth-title { margin: 0 0 0.4rem; font-size: 1.25rem; }
.auth-sub { margin: 0 0 1.25rem; color: var(--muted); font-size: 0.95rem; }
.auth-resend { text-align: center; margin-top: 0.5rem; }

/* ---- App shell ---- */
.topbar { background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.topbar__inner {
    max-width: var(--content-width);
    margin: 0 auto;
    padding: 0.6rem 1rem;
    display: flex;
    align-items: center;
    gap: 1rem;
}
.topbar__brand { display: flex; align-items: center; gap: 0.5rem; font-weight: 700; color: var(--text); text-decoration: none; }
.topbar__brand .auth-logo { width: 1.8rem; height: 1.8rem; font-size: 1rem; border-radius: 8px; }
.topbar__toggle { margin-left: auto; background: none; border: 1px solid var(--line); color: var(--text); border-radius: 8px; padding: 0.35rem 0.6rem; font-size: 1.1rem; cursor: pointer; display: none; }

.mainnav { margin-left: auto; display: flex; align-items: center; gap: 0.25rem; }
.mainnav__link {
    color: var(--muted);
    text-decoration: none;
    padding: 0.5rem 0.75rem;
    border-radius: 8px;
    font-size: 0.95rem;
}
.mainnav__link:hover { color: var(--text); background: var(--surface-2); }
.mainnav__link.is-active { color: var(--text); background: var(--surface-2); font-weight: 600; }
.mainnav__logout { margin-left: 0.25rem; }

.content { max-width: var(--content-width); margin: 0 auto; padding: 1.5rem 1rem 4rem; }

/* ---- Page head + stats ---- */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 1.6rem; letter-spacing: -0.02em; }

.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 0.75rem 1rem; display: flex; flex-direction: column; gap: 0.15rem; }
.stat__label { color: var(--muted); font-size: 0.8rem; }
.stat__value { font-size: 1.35rem; font-weight: 700; }
.stat__value--sm { font-size: 1.05rem; font-weight: 600; }
.stat--accent { border-color: var(--accent); }
.stat--accent .stat__value { color: var(--accent); }
.stat--warn { border-color: var(--warn); }
.stat--warn .stat__value { color: var(--warn); }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 0.75rem; margin-bottom: 1.25rem; }

/* ---- Cards ---- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1.25rem; }
.card__title { margin: 0 0 1rem; font-size: 1.1rem; }
.card__head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }
.card__head .card__title { margin: 0; }

/* ---- Tables ---- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
.table th, .table td { text-align: left; padding: 0.55rem 0.6rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.table thead th { color: var(--muted); font-weight: 600; font-size: 0.82rem; text-transform: uppercase; letter-spacing: 0.03em; }
.table tfoot th { border-top: 2px solid var(--line); border-bottom: none; font-size: 0.95rem; }
.table .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.table--compact th, .table--compact td { padding: 0.4rem 0.5rem; font-size: 0.85rem; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, "SFMono-Regular", Menlo, monospace; font-size: 0.85rem; }
.actions-col { text-align: right; white-space: nowrap; }
.is-inactive td { opacity: 0.55; }
.empty { color: var(--muted); margin: 0.5rem 0; }
.hint { color: var(--muted); font-size: 0.85rem; margin: -0.5rem 0 1.25rem; }
.badge { display: inline-block; font-size: 0.72rem; padding: 0.1rem 0.45rem; border-radius: 999px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); }

/* ---- Buttons (small) ---- */
.btn--sm { padding: 0.3rem 0.6rem; font-size: 0.82rem; }
.inline { display: inline; }

/* ---- Forms: rows & stacks ---- */
.form-row { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: flex-end; }
.form-row .field { flex: 1 1 12rem; margin-bottom: 0; }
.form-row .field--amount { flex: 0 0 9rem; }
.form-row__submit { flex: 0 0 auto; }
.form-row--tight { margin-top: 1rem; padding-top: 1rem; border-top: 1px dashed var(--line); }

.form-stack .field { margin-bottom: 1rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; }

/* ---- Category management ---- */
.cat-manage { display: grid; gap: 0.5rem; margin-bottom: 0.5rem; }
.cat-manage__row { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.cat-manage__edit { display: flex; gap: 0.5rem; align-items: center; }
.cat-manage__edit input { max-width: 16rem; }

/* ---- Columns ---- */
.cols { display: grid; gap: 1.25rem; }
.cols--2 { grid-template-columns: 1fr 1fr; }
.cols .card { margin-bottom: 0; }

/* ---- Chart (donut + legend) ---- */
.chart { display: flex; align-items: center; gap: 1.25rem; flex-wrap: wrap; }
.chart__donut { flex: 0 0 auto; width: 150px; }
.donut { width: 150px; height: 150px; }
.donut-seg { transition: none; }
.legend { list-style: none; margin: 0; padding: 0; flex: 1 1 12rem; display: grid; gap: 0.4rem; }
.legend__item { display: grid; grid-template-columns: 12px 1fr auto; align-items: center; gap: 0.5rem; font-size: 0.9rem; }
.legend__val { color: var(--muted); font-size: 0.82rem; white-space: nowrap; font-variant-numeric: tabular-nums; }
.swatch { width: 12px; height: 12px; display: block; }

.strong { font-weight: 700; }
.neg { color: var(--fail); }

/* ---- Responsive ---- */
@media (max-width: 860px) {
    .cols--2 { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
    .topbar__toggle { display: block; }
    .mainnav {
        display: none;
        position: absolute;
        top: 100%; left: 0; right: 0;
        flex-direction: column;
        align-items: stretch;
        background: var(--surface);
        border-bottom: 1px solid var(--line);
        padding: 0.5rem;
        gap: 0.15rem;
    }
    .mainnav.is-open { display: flex; }
    .mainnav__logout { margin: 0.25rem 0 0; }
    .grid-2 { grid-template-columns: 1fr; }
}
