:root {
    --bg: #f6f3f1;
    --surface: #ffffff;
    --text: #1f1a1c;
    --muted: #6f6669;
    --line: #e7e0dd;
    --accent: #c8203f;
    --accent-ink: #ffffff;
    --accent-soft: #fbe9ec;
    --good: #1d7a4a;
    --bad: #b3261e;
    --radius: 14px;
    --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 2px 8px rgba(0, 0, 0, .04);
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #141113;
        --surface: #1f1b1d;
        --text: #f2eced;
        --muted: #a79da0;
        --line: #332d30;
        --accent: #ff5c7a;
        --accent-ink: #1a0a0e;
        --accent-soft: #3a1d24;
        --good: #6fd39c;
        --bad: #ff8a80;
        --shadow: none;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    padding-bottom: env(safe-area-inset-bottom);
}

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

h1 { font-size: 1.4rem; margin: 0; }
h2 { font-size: 1.05rem; margin: 28px 0 10px; display: flex; align-items: center; gap: 8px; }

.container { max-width: 760px; margin: 0 auto; padding: 16px; }
.container.narrow { max-width: 520px; }
.center { text-align: center; }

/* Top bar */
.topbar {
    position: sticky; top: 0; z-index: 10;
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
    background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); font-weight: 600; }
.brand img { border-radius: 50%; object-fit: cover; }
.tabs { display: flex; align-items: center; gap: 4px; }
.tabs a, .tabs .link {
    padding: 8px 12px; border-radius: 999px; color: var(--muted); font: inherit; font-size: .95rem;
}
.tabs a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.inline { display: inline; margin: 0; }
.link { background: none; border: 0; cursor: pointer; }

/* Period switcher */
.period { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 8px 0 12px; }
.period h1 { flex: 1; text-align: center; }
.icon-btn {
    display: grid; place-items: center; width: 44px; height: 44px; border-radius: 50%;
    background: var(--surface); box-shadow: var(--shadow); font-size: 1.6rem; line-height: 1; color: var(--text);
}

/* Summary cards */
.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.card { background: var(--surface); border-radius: var(--radius); padding: 12px; box-shadow: var(--shadow); min-width: 0; }
.card-label { color: var(--muted); font-size: .8rem; }
.card-value { font-size: 1.15rem; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card.accent { background: var(--accent-soft); }
.card.accent .card-value { color: var(--good); }
.card.loss .card-value { color: var(--bad); }

@media (max-width: 420px) {
    .cards { grid-template-columns: 1fr 1fr; }
    .cards .card.accent { grid-column: span 2; }
}

/* Buttons */
.actions { display: grid; grid-template-columns: 2fr 1fr; gap: 10px; margin-top: 16px; }
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    min-height: 48px; padding: 10px 18px; border-radius: 12px; border: 1px solid var(--line);
    background: var(--surface); color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--bad); border-color: transparent; background: transparent; margin-top: 8px; }
.btn.block { width: 100%; }
.btn:active { transform: scale(.98); }

/* Lists */
.badge { background: var(--line); color: var(--muted); border-radius: 999px; padding: 0 8px; font-size: .8rem; font-weight: 600; }
.empty { color: var(--muted); background: var(--surface); border-radius: var(--radius); padding: 16px; text-align: center; margin: 0; }
.list { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.list li + li { border-top: 1px solid var(--line); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 14px; color: var(--text); min-height: 56px; }
.row:active { background: var(--accent-soft); }
.row-main { display: flex; flex-direction: column; min-width: 0; }
.row-title { font-weight: 500; overflow-wrap: anywhere; }
.row-sub { color: var(--muted); font-size: .85rem; }
.row-amount { font-weight: 700; white-space: nowrap; }
.minus { color: var(--bad); }

/* Forms */
.back { display: inline-block; margin: 4px 0 8px; padding: 6px 0; }
.form { display: grid; gap: 16px; margin: 16px 0; }
.form label { display: grid; gap: 6px; }
.form label > span { font-weight: 600; font-size: .95rem; }
.form em { font-weight: 400; color: var(--muted); font-style: normal; }
.form input[type=text], .form input[type=password], .form input[type=datetime-local] {
    -webkit-appearance: none; appearance: none;
    width: 100%; min-height: 48px; padding: 10px 12px; border-radius: 12px;
    border: 1px solid var(--line); background: var(--surface); color: var(--text); font: inherit; font-size: 1.05rem;
}
.form input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.form input.invalid { border-color: var(--bad); }
.form .check { display: flex; align-items: center; gap: 10px; font-weight: 400; }
.form .check input { width: 20px; height: 20px; accent-color: var(--accent); }
.form .check span { font-weight: 400; }
.hint { color: var(--muted); }
.error { color: var(--bad); }

/* Year table */
.table-wrap { overflow-x: auto; margin-top: 16px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.table { width: 100%; border-collapse: collapse; font-size: .95rem; }
.table th, .table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table thead th { color: var(--muted); font-weight: 600; font-size: .8rem; }
.table tfoot th { border-bottom: 0; }
.table .num { text-align: right; }
.table tr.muted td { color: var(--muted); }

/* Toast */
.toast {
    position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
    background: var(--text); color: var(--bg); padding: 12px 18px; border-radius: 12px; z-index: 20;
    box-shadow: 0 6px 24px rgba(0, 0, 0, .2); max-width: calc(100% - 32px);
    animation: toast 3.5s ease forwards;
}
@keyframes toast {
    0% { opacity: 0; transform: translate(-50%, 10px); }
    8%, 85% { opacity: 1; transform: translate(-50%, 0); }
    100% { opacity: 0; visibility: hidden; }
}

/* Login / error */
.login-page { display: grid; place-items: center; min-height: 100vh; min-height: 100dvh; padding: 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border-radius: 20px; padding: 28px 24px; box-shadow: var(--shadow); text-align: center; }
.login-card .form { text-align: left; }
.login-logo { border-radius: 50%; object-fit: cover; }
.alert { background: #fdecea; color: var(--bad); padding: 10px 12px; border-radius: 10px; }
.note { color: var(--muted); }

/* htmx request indicator: dim the page while navigating */
.htmx-request main { opacity: .6; transition: opacity .2s; }

/* Small phones */
@media (max-width: 400px) {
    .container { padding: 12px; }
    .topbar { padding-left: 12px; padding-right: 12px; }
    .brand span { display: none; }
    .tabs a, .tabs .link { padding: 8px 10px; }
    .card-value { font-size: 1.05rem; }
    .table th, .table td { padding: 10px 8px; }
    .hide-sm { display: none; }
}
/* iOS: datetime-local has no intrinsic width and renders centred */
.form input[type=datetime-local] { display: block; text-align: left; }
.form input[type=datetime-local]::-webkit-date-and-time-value { text-align: left; }
