:root {
    --bg: #faf7f4;
    --bg-subtle: #f3efe9;
    --white: #fff;
    --border: #e7e2dd;
    --border-light: #f0ebe5;
    --text: #1c1917;
    --muted: #78716c;
    --accent: #d97706;
    --accent-dark: #b45309;
    --accent-light: #fff7ed;
    --accent-muted: #fdba74;
    --blue: #d97706;
    --blue-dark: #b45309;
    --radius: 12px;
    --radius-sm: 8px;
    --radius-lg: 16px;
    --row-h: 40px;
    --shadow-sm: 0 1px 2px rgba(28, 25, 23, 0.04);
    --shadow: 0 2px 8px rgba(28, 25, 23, 0.06);
    --shadow-lg: 0 4px 20px rgba(28, 25, 23, 0.08);
    --font-num: system-ui, -apple-system, "Segoe UI", sans-serif;
}

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

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

body {
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.5;
    padding-bottom: env(safe-area-inset-bottom, 0);
    -webkit-tap-highlight-color: transparent;
}

.app-body { min-height: 100vh; min-height: 100dvh; }

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

.wrap { max-width: 900px; margin: 0 auto; padding: 0 1rem; }

/* Top bar */
.topbar {
    background: var(--white);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    position: sticky;
    top: 0;
    z-index: 10;
    padding-top: env(safe-area-inset-top, 0);
}
.topbar__inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    min-height: 48px;
    padding: 0.5rem 0;
}
.topbar__logo {
    display: none;
    font-weight: 700;
    font-size: 1.05rem;
    color: var(--text);
    flex-shrink: 0;
    letter-spacing: -0.02em;
}
.topbar__logo:hover { text-decoration: none; color: var(--accent); }
.topbar__pagetitle {
    flex: 1;
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}
.topbar__actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-shrink: 0;
}
.topbar__logout {
    color: var(--muted);
    font-weight: 600;
    font-size: 0.78rem;
    padding: 0.35rem 0.25rem;
}
.topbar__logout:hover { color: var(--text); text-decoration: none; }

/* Desktop nav */
.topnav { display: none; }
.topnav__inner {
    display: flex;
    align-items: center;
    gap: 0.15rem;
}
.topnav__item {
    display: inline-flex;
    align-items: center;
    padding: 0.45rem 0.85rem;
    border-radius: var(--radius-sm);
    color: var(--muted);
    font-size: 0.85rem;
    font-weight: 600;
    text-decoration: none;
    white-space: nowrap;
    position: relative;
}
.topnav__item:hover { background: var(--bg-subtle); color: var(--text); text-decoration: none; }
.topnav__item.is-active {
    color: var(--text);
    background: var(--accent-light);
}
.topnav__item.is-active::after {
    content: '';
    position: absolute;
    bottom: 0.15rem;
    left: 50%;
    transform: translateX(-50%);
    width: 1.25rem;
    height: 2px;
    background: var(--accent);
    border-radius: 1px;
}
.topnav__icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.35rem;
    height: 0.9rem;
    margin-right: 0.35rem;
    flex-shrink: 0;
}
.topnav__icon svg {
    display: block;
    width: 100%;
    height: 100%;
}

/* Mobile bottom tab bar */
.tabbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    background: rgba(255, 255, 255, 0.97);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-top: 1px solid var(--border);
    box-shadow: 0 -2px 12px rgba(28, 25, 23, 0.04);
    padding-left: 0.5rem;
    padding-right: 0.5rem;
    padding-bottom: env(safe-area-inset-bottom, 0);
}
.tabbar__inner {
    display: flex;
    align-items: stretch;
    justify-content: center;
    gap: 0.15rem;
    min-height: 58px;
    padding: 0.35rem 0.25rem;
}
.tabbar__item {
    flex: 1;
    max-width: 7.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.2rem;
    padding: 0.35rem 0.4rem;
    border-radius: var(--radius-sm);
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 600;
    text-decoration: none;
    position: relative;
}
.tabbar__icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.4rem;
    height: 1.4rem;
    flex-shrink: 0;
}
.tabbar__icon svg {
    display: block;
    width: 100%;
    height: 100%;
}
.tabbar__icon svg[stroke] {
    overflow: visible;
}
.tabbar__item:hover {
    color: var(--text);
    text-decoration: none;
    background: var(--bg-subtle);
}
.tabbar__item.is-active {
    color: var(--text);
    background: var(--accent-light);
}
.tabbar__item.is-active::before {
    content: '';
    position: absolute;
    top: 0;
    left: 20%;
    right: 20%;
    height: 2.5px;
    background: var(--accent);
    border-radius: 0 0 3px 3px;
}
.tabbar__item.is-active .tabbar__icon {
    color: var(--accent);
}
.tabbar__label {
    white-space: nowrap;
    font-size: 0.62rem;
    line-height: 1.1;
    letter-spacing: 0.01em;
}
@media (max-width: 359px) {
    .tabbar__label--short { display: none; }
    .tabbar__item { padding: 0.35rem 0.3rem; }
}

.badge {
    display: inline-block;
    padding: 0.15rem 0.45rem;
    border-radius: 999px;
    font-size: 0.62rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.badge--view { background: var(--accent-light); color: var(--accent-dark); border: 1px solid var(--accent-muted); }

.main {
    padding: 1rem 1rem calc(4.5rem + env(safe-area-inset-bottom, 0));
}

/* Typography */
h1 {
    font-size: 1.35rem;
    font-weight: 700;
    margin-bottom: 0.15rem;
    line-height: 1.2;
    letter-spacing: -0.02em;
}
h2 {
    font-size: 0.95rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    letter-spacing: -0.01em;
}
.subtitle { color: var(--muted); margin-bottom: 0.85rem; font-size: 0.88rem; }

/* Cards */
.card {
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    margin-bottom: 0.75rem;
    box-shadow: var(--shadow-sm);
}
.card--highlight {
    border-color: var(--accent-muted);
    background: linear-gradient(180deg, var(--white) 0%, var(--accent-light) 100%);
}

/* Summary / hero */
.summary-wrap {
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
    margin-bottom: 0.85rem;
}
.summary-wrap--hero {
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1.15rem 1rem;
    box-shadow: var(--shadow);
    margin-bottom: 1rem;
}
.summary {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
}
.summary--dense { margin-bottom: 0; }
.summary__item {
    background: var(--bg-subtle);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    padding: 0.6rem 0.65rem;
    min-width: 0;
}
.summary__label {
    font-size: 0.65rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    line-height: 1.2;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.summary__value {
    font-family: var(--font-num);
    font-size: 1rem;
    font-weight: 700;
    margin-top: 0.2rem;
    line-height: 1.15;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.02em;
}
.summary__sub { font-size: 0.72rem; color: var(--muted); margin-top: 0.2rem; }
.summary__item--net {
    background: var(--accent-light);
    border-color: var(--accent-muted) !important;
    padding: 0.85rem 1rem;
    grid-column: 1 / -1;
}
.summary__item--net .summary__label {
    color: var(--accent-dark);
    font-size: 0.7rem;
}
.summary__item--net .summary__value {
    color: var(--text);
    font-size: 1.65rem;
    margin-top: 0.25rem;
}
.summary__value--positive { color: var(--text) !important; }
.summary__value--negative { color: var(--muted) !important; }

/* Region cards (home) */
.region-cards { display: grid; grid-template-columns: 1fr; gap: 0.85rem; }
.region-card {
    padding: 1.15rem;
    transition: box-shadow 0.15s ease;
}
.region-card:hover { box-shadow: var(--shadow); }
.region-card__head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 0.75rem;
    margin-bottom: 0.85rem;
}
.region-card__title { font-size: 1.05rem; font-weight: 700; letter-spacing: -0.01em; }
.region-card__month { font-size: 0.8rem; color: var(--muted); margin-top: 0.15rem; }
.region-card .summary__item--net {
    margin-bottom: 0.5rem;
}
.region-card .summary__item--net .summary__value {
    font-size: 1.5rem;
}

/* Lists — banking transaction style */
.list { list-style: none; }
.list__row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.65rem 0;
    border-bottom: 1px solid var(--border-light);
    flex-wrap: nowrap;
    min-height: var(--row-h);
}
.list__row:last-child { border-bottom: none; }
.list__row--done { opacity: 0.72; }
.list__row--done .list__name { text-decoration: line-through; color: var(--muted); }

.list__name {
    font-weight: 500;
    font-size: 0.9rem;
    line-height: 1.25;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}
.list__content {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 0.5rem;
}
.list__details {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    flex-shrink: 0;
    margin-left: auto;
}
.list__amount {
    font-family: var(--font-num);
    font-size: 0.92rem;
    white-space: nowrap;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.01em;
}
.list__actions {
    display: flex;
    align-items: center;
    gap: 0.15rem;
    flex-shrink: 0;
}

/* Forms */
.input, .select {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.45rem 0.6rem;
    font-size: 16px;
    background: var(--white);
    color: var(--text);
    min-height: var(--row-h);
    line-height: 1.2;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.input:focus, .select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(217, 119, 6, 0.15);
}
.input--amount { width: 5.5rem; text-align: right; flex-shrink: 0; font-variant-numeric: tabular-nums; }
.input--grow { flex: 1; min-width: 0; width: auto; }
.select--fluid { width: auto; min-width: 8rem; flex: 1; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--row-h);
    padding: 0.45rem 0.85rem;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 600;
    border: 1px solid transparent;
    cursor: pointer;
    text-decoration: none;
    background: none;
    white-space: nowrap;
    transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.btn--primary {
    background: var(--accent);
    color: #fff;
    box-shadow: 0 1px 3px rgba(217, 119, 6, 0.25);
}
.btn--primary:hover {
    background: var(--accent-dark);
    color: #fff;
    text-decoration: none;
    box-shadow: 0 2px 6px rgba(217, 119, 6, 0.3);
}
.btn--ghost {
    border-color: var(--border);
    color: var(--text);
    background: var(--white);
}
.btn--ghost:hover { background: var(--bg-subtle); text-decoration: none; }
.btn--sm { min-height: 36px; padding: 0.35rem 0.7rem; font-size: 0.8rem; }
.btn--xs { min-height: 32px; padding: 0.2rem 0.5rem; font-size: 0.75rem; }
.btn--danger {
    color: var(--muted);
    border: none;
    font-size: 1.2rem;
    line-height: 1;
    min-width: 32px;
    min-height: 32px;
    padding: 0;
    cursor: pointer;
    background: transparent;
    flex-shrink: 0;
    border-radius: var(--radius-sm);
}
.btn--danger:hover { color: var(--text); background: var(--bg-subtle); }

.section-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
    padding-bottom: 0.35rem;
    border-bottom: 1px solid var(--border-light);
}
.section-head h2 { margin-bottom: 0; }

.add-row {
    display: flex;
    gap: 0.45rem;
    flex-wrap: wrap;
    align-items: center;
    padding: 0.65rem;
    background: var(--bg-subtle);
    border-radius: var(--radius-sm);
    margin-bottom: 0.55rem;
    border: 1px dashed var(--border);
}
.add-row .input--grow { flex: 1 1 6rem; min-width: 5rem; width: auto; }
.add-row .input--amount { width: 5.5rem; }
.add-row .input:not(.input--grow):not(.input--amount) { flex: 1 1 5rem; width: auto; min-width: 4.5rem; }
.add-row .select { width: auto; flex: 0 1 auto; }
.hidden { display: none !important; }

.check {
    width: 32px;
    height: 32px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--white);
    cursor: pointer;
    font-size: 0.85rem;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s ease, border-color 0.15s ease;
}
.check--on {
    background: var(--accent-light);
    border-color: var(--accent);
    color: var(--accent-dark);
    font-weight: 700;
}

.tag {
    font-size: 0.68rem;
    background: var(--bg-subtle);
    padding: 0.15rem 0.45rem;
    border-radius: 999px;
    color: var(--muted);
    white-space: nowrap;
    border: 1px solid var(--border-light);
    font-weight: 500;
}

.notice {
    padding: 0.7rem 0.9rem;
    border-radius: var(--radius-sm);
    margin-bottom: 0.85rem;
    font-size: 0.88rem;
}
.notice--success { background: var(--accent-light); border: 1px solid var(--accent-muted); color: var(--accent-dark); }
.notice--error { background: #fffbeb; border: 1px solid #fcd34d; color: #92400e; }

.page-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.85rem;
    flex-wrap: nowrap;
}
.page-head__actions {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    flex-shrink: 0;
}
.month-picker { flex-shrink: 1; min-width: 0; }

.table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -0.25rem;
    padding: 0 0.25rem;
}
.table { width: 100%; border-collapse: collapse; font-size: 0.85rem; min-width: 480px; }
.table th, .table td {
    padding: 0.55rem 0.5rem;
    text-align: left;
    border-bottom: 1px solid var(--border-light);
}
.table th {
    font-size: 0.68rem;
    text-transform: uppercase;
    color: var(--muted);
    font-weight: 600;
    white-space: nowrap;
    letter-spacing: 0.03em;
}
.table td strong {
    font-family: var(--font-num);
    font-variant-numeric: tabular-nums;
}
.table__stacked { padding: 0 !important; border: none !important; }

.progress { margin-bottom: 0.85rem; }
.progress__label {
    display: flex;
    justify-content: space-between;
    gap: 0.5rem;
    font-size: 0.8rem;
    color: var(--muted);
    margin-bottom: 0.35rem;
    font-weight: 500;
}
.progress__label span:last-child {
    font-family: var(--font-num);
    font-weight: 700;
    color: var(--text);
    font-variant-numeric: tabular-nums;
}
.progress__bar {
    height: 8px;
    background: var(--bg-subtle);
    border-radius: 4px;
    overflow: hidden;
    border: 1px solid var(--border-light);
}
.progress__fill {
    height: 100%;
    background: linear-gradient(90deg, var(--accent-muted), var(--accent));
    border-radius: 3px;
}

.tabs {
    display: flex;
    gap: 0.15rem;
    margin-bottom: 0.85rem;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
    display: inline-flex;
    align-items: center;
    padding: 0.5rem 0.85rem;
    color: var(--muted);
    font-weight: 600;
    font-size: 0.85rem;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    white-space: nowrap;
    flex-shrink: 0;
}
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.active {
    color: var(--text);
    border-bottom-color: var(--accent);
    background: var(--accent-light);
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
}

.empty {
    text-align: center;
    padding: 2rem 1rem;
    color: var(--muted);
    background: var(--white);
    border: 1px dashed var(--border);
    border-radius: var(--radius);
}
.empty p { margin-bottom: 0.85rem; }

.edit-panel-row { border-bottom: none !important; padding: 0 !important; }
.edit-row {
    padding: 0.65rem;
    background: var(--bg-subtle);
    border-radius: var(--radius-sm);
    margin: 0.25rem 0 0.45rem;
    border: 1px solid var(--border-light);
}
.edit-row form {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem;
    align-items: center;
}
.edit-row .input--grow { flex: 1 1 6rem; width: auto; }
.edit-row .input:not(.input--grow):not(.input--amount) { flex: 1 1 4.5rem; width: auto; }

.toggle-form, .delete-form { flex-shrink: 0; }
.item-form {
    display: flex;
    flex: 1;
    gap: 0.45rem;
    align-items: center;
    min-width: 0;
    flex-wrap: nowrap;
}
.item-form--template { flex-wrap: wrap; }

.tpl-check {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.78rem;
    color: var(--muted);
    cursor: pointer;
    white-space: nowrap;
}
.tpl-check input { width: 1rem; height: 1rem; cursor: pointer; accent-color: var(--accent); }

.plan-balances {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem 0.6rem;
    margin-top: 0.35rem;
    padding-left: 1.35rem;
}
.plan-balances__item {
    font-size: 0.78rem;
    color: var(--muted);
    white-space: nowrap;
}

.toast-container {
    position: fixed;
    left: 0.75rem;
    right: 0.75rem;
    bottom: calc(4rem + env(safe-area-inset-bottom, 0));
    z-index: 200;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    pointer-events: none;
    align-items: center;
}
.toast {
    background: var(--text);
    color: #fff;
    padding: 0.65rem 1rem;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 500;
    box-shadow: var(--shadow-lg);
    opacity: 0;
    transform: translateY(6px);
    transition: opacity 0.2s, transform 0.2s;
    max-width: 320px;
    width: auto;
}
.toast--show { opacity: 1; transform: translateY(0); }
.toast--error { background: var(--accent-dark); }

/* Login */
.login-page {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.25rem;
    background: linear-gradient(165deg, var(--bg) 0%, var(--accent-light) 100%);
}
.login-card {
    width: 100%;
    max-width: 380px;
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1.75rem 1.5rem;
    box-shadow: var(--shadow-lg);
}
.login-card h1 { font-size: 1.5rem; margin-bottom: 0.25rem; }
.login-form { display: flex; flex-direction: column; gap: 0.85rem; margin-top: 1rem; }
.login-form__field { display: flex; flex-direction: column; gap: 0.3rem; }
.login-form__field span { font-size: 0.82rem; color: var(--muted); font-weight: 600; }
.login-form .input { width: 100%; }
.btn--block { width: 100%; min-height: 44px; }

.list__row--template {
    flex-wrap: wrap;
    padding: 0.55rem 0;
    border-bottom: 1px solid var(--border-light);
}
.list__row--template .item-form--template { width: 100%; }
.list__row--template .delete-form { margin-left: auto; }

.table--templates { min-width: 0; }
.table--templates thead { display: none; }

/* Budget hero wrapper */
.budget-hero { margin-bottom: 0.25rem; }

@media (min-width: 640px) {
    body { font-size: 15px; }
    .wrap { padding: 0 1.25rem; }
    .main { padding: 1.25rem 1.25rem calc(4.75rem + env(safe-area-inset-bottom, 0)); }
    h1 { font-size: 1.6rem; }
    h2 { font-size: 1.05rem; }
    .card { padding: 1.15rem 1.25rem; margin-bottom: 1rem; }
    .region-cards { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1rem; }
    .tabbar__item { font-size: 0.82rem; max-width: 8.5rem; }
    .summary { grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 0.6rem; }
    .summary__value { font-size: 1.05rem; }
    .summary__item--net .summary__value { font-size: 1.85rem; }
    .summary-wrap--hero { padding: 1.35rem 1.25rem; }
    .input--amount { width: 6.5rem; }
    .list__row { padding: 0.7rem 0; gap: 0.6rem; }
    .toast-container { left: auto; right: 1rem; bottom: calc(4.25rem + env(safe-area-inset-bottom, 0)); align-items: flex-end; }

    .list__row--template {
        flex-wrap: nowrap;
        border-bottom: none;
        padding: 0;
    }
    .list__row--template .item-form--template { flex-wrap: wrap; width: auto; }
    .table--templates { min-width: 520px; }
    .table--templates thead { display: table-header-group; }
}

@media (min-width: 768px) {
    .wrap { padding: 0 1.5rem; }

    .topbar__logo { display: block; }
    .topbar__pagetitle { display: none; }
    .topbar__inner { gap: 1.25rem; padding: 0.65rem 0; }

    .topnav {
        display: block;
        flex: 1;
        min-width: 0;
    }
    .topnav__inner { justify-content: center; }

    .tabbar { display: none; }

    .main {
        padding: 1.5rem 1.5rem 2.5rem;
    }

    .toast-container {
        bottom: calc(1rem + env(safe-area-inset-bottom, 0));
    }

    .region-card .summary__item--net .summary__value {
        font-size: 1.75rem;
    }
}

@media (display-mode: standalone) {
    .topbar { box-shadow: var(--shadow-sm); }
    .tabbar { background: rgba(255, 255, 255, 0.98); }
}
