:root {
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --income: #16a34a;
    --expense: #dc2626;
    --bg: #f3f4f8;
    --card: #ffffff;
    --text: #111827;
    --muted: #6b7280;
    --border: #e5e7eb;
    --radius: 16px;
    --nav-height: 64px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { margin: 0; padding: 0; }

html {
    /* Canvas colour shown below the body (e.g. iOS home-indicator area); matches the bottom nav. */
    background: var(--card);
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    touch-action: manipulation;
}

body {
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.45;
}

a { color: inherit; text-decoration: none; }

h1 { font-size: 1.35rem; margin: 0; }
h2 { font-size: 1rem; margin: 0 0 12px; }

.container {
    width: 100%;
    max-width: 520px;
    margin: 0 auto;
    padding: 16px 16px 24px;
    flex: 1 0 auto;
}

/* Column layout: content grows, so the sticky bottom nav always sits at the bottom on short pages. */
body {
    display: flex;
    flex-direction: column;
}

/* Header */
.app-header {
    flex-shrink: 0;
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: calc(14px + env(safe-area-inset-top)) 16px 14px;
    background: var(--primary);
    color: #fff;
}
.app-title { font-weight: 700; display: flex; gap: 8px; align-items: center; }
.app-user { font-size: 0.85rem; opacity: 0.9; }

/* Bottom nav */
.bottom-nav {
    position: -webkit-sticky;
    position: sticky;
    bottom: 0;
    flex-shrink: 0;
    margin-left: calc(-1 * env(safe-area-inset-left));
    margin-right: calc(-1 * env(safe-area-inset-right));
    z-index: 30;
    height: calc(var(--nav-height) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    align-items: center;
    background: var(--card);
    border-top: 1px solid var(--border);
}
.bottom-nav a {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    font-size: 0.7rem;
    color: var(--muted);
}
.bottom-nav a i { font-size: 1.3rem; }
.bottom-nav a.active { color: var(--primary); font-weight: 600; }
.bottom-nav .nav-add {
    width: 52px; height: 52px;
    margin: -22px auto 0;
    border-radius: 50%;
    justify-content: center;
    background: var(--primary);
    color: #fff;
    box-shadow: 0 6px 16px rgba(79, 70, 229, 0.4);
}
.bottom-nav .nav-add i { font-size: 1.6rem; }

/* Cards */
.card {
    background: var(--card);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 14px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}
.card.flush { padding: 4px 16px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; }
.link { color: var(--primary); font-size: 0.85rem; font-weight: 600; }
.empty { color: var(--muted); text-align: center; margin: 8px 0; }
.muted { color: var(--muted); }

.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.page-head .btn { margin-left: auto; }
.back {
    width: 36px; height: 36px;
    display: inline-flex; align-items: center; justify-content: center;
    background: var(--card); border-radius: 50%;
}

/* Month switch */
.month-switch {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: 14px; font-weight: 600; text-transform: capitalize;
}
.month-switch a {
    width: 38px; height: 38px;
    display: inline-flex; align-items: center; justify-content: center;
    background: var(--card); border-radius: 50%;
}

/* Balance card */
.balance-card {
    background: linear-gradient(135deg, var(--primary), #7c3aed);
    color: #fff;
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 14px;
}
.balance-label { opacity: 0.8; font-size: 0.85rem; }
.balance-value { font-size: 1.9rem; font-weight: 700; margin: 4px 0 16px; word-break: break-word; }
.balance-value.negative { color: #fecaca; }
.balance-split { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.balance-split > div { background: rgba(255, 255, 255, 0.15); border-radius: 12px; padding: 10px 12px; }
.split-label { display: block; font-size: 0.78rem; opacity: 0.85; }
.split-value { font-weight: 600; font-size: 0.95rem; word-break: break-word; }

.quick-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }

.summary-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.summary-box { background: var(--card); border-radius: 12px; padding: 10px 12px; }
.summary-box span { display: block; font-size: 0.78rem; color: var(--muted); }
.summary-box strong { font-size: 0.95rem; word-break: break-word; }
.summary-box.income strong { color: var(--income); }
.summary-box.expense strong { color: var(--expense); }

/* Segmented */
.segmented {
    display: grid; grid-template-columns: repeat(3, 1fr);
    background: #e5e7eb; border-radius: 12px; padding: 3px; margin-bottom: 8px;
}
.segmented a { text-align: center; padding: 8px 0; border-radius: 10px; font-weight: 500; color: var(--muted); }
.segmented a.active { background: var(--card); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); }

.day-title { margin: 14px 4px 6px; font-size: 0.8rem; font-weight: 600; color: var(--muted); text-transform: capitalize; }

/* List items */
.list-item {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
}
.list-item:last-child { border-bottom: none; }
.list-item form { margin: 0; }
.icon-circle {
    flex: 0 0 40px; width: 40px; height: 40px;
    display: inline-flex; align-items: center; justify-content: center;
    border-radius: 50%; color: #fff; font-size: 1.15rem;
}
.item-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.item-title { font-weight: 600; }
.item-sub { font-size: 0.8rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-amount { font-weight: 600; white-space: nowrap; }
.item-amount.income { color: var(--income); }
.item-amount.expense { color: var(--expense); }

.icon-btn {
    width: 36px; height: 36px;
    display: inline-flex; align-items: center; justify-content: center;
    background: #f3f4f6; border: none; border-radius: 50%;
    color: var(--muted); font-size: 1rem; cursor: pointer;
}
.icon-btn.danger { color: var(--expense); }

/* Bars */
.bar-row { margin-bottom: 12px; }
.bar-head { display: flex; justify-content: space-between; font-size: 0.88rem; margin-bottom: 4px; }
.bar-track { height: 8px; background: #eef0f4; border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 99px; }

/* Forms */
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-weight: 500; font-size: 0.85rem; margin-bottom: 6px; }

input[type=text], input[type=password], input[type=number], input[type=date], select, input:not([type]) {
    width: 100%;
    padding: 13px 14px;
    font: inherit;
    font-size: 16px; /* prevents iOS zoom */
    color: var(--text);
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 12px;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}
input[type=date] { min-height: 50px; }
button, a, label { touch-action: manipulation; }
button { -webkit-appearance: none; appearance: none; }
input:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15); }
.amount-input { font-size: 1.5rem !important; font-weight: 700; }
.color-input { height: 48px; padding: 4px !important; }

.checkbox-row { display: flex; align-items: center; gap: 8px; margin-bottom: 16px; }
.checkbox-row input { width: 18px; height: 18px; }

.field-error { display: block; color: var(--expense); font-size: 0.8rem; margin-top: 4px; }
.validation-summary-valid { display: none; }

.type-toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 16px; }
.type-toggle input { display: none; }
.type-toggle span {
    display: flex; align-items: center; justify-content: center; gap: 6px;
    padding: 12px; border-radius: 12px; border: 2px solid var(--border);
    font-weight: 600; cursor: pointer; color: var(--muted);
}
.type-toggle .expense input:checked + span { border-color: var(--expense); color: var(--expense); background: #fef2f2; }
.type-toggle .income input:checked + span { border-color: var(--income); color: var(--income); background: #f0fdf4; }

.icon-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(48px, 1fr)); gap: 8px; }
.icon-grid input { display: none; }
.icon-grid span {
    height: 48px; display: flex; align-items: center; justify-content: center;
    border: 2px solid var(--border); border-radius: 12px; font-size: 1.3rem; cursor: pointer; color: var(--muted);
}
.icon-grid input:checked + span { border-color: var(--primary); color: var(--primary); background: #eef2ff; }

/* Buttons */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 13px 16px; font: inherit; font-weight: 600;
    border: none; border-radius: 12px; cursor: pointer;
}
.btn-block { width: 100%; }
.btn-small { padding: 8px 12px; font-size: 0.85rem; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); }
.btn-secondary { background: #e0e7ff; color: var(--primary); }
.btn-danger { background: #fee2e2; color: var(--expense); }
.btn-income { background: #dcfce7; color: var(--income); }
.btn-expense { background: #fee2e2; color: var(--expense); }

/* Alerts */
.alert { padding: 12px 14px; border-radius: 12px; margin-bottom: 14px; font-size: 0.9rem; }
.alert-success { background: #dcfce7; color: #166534; }
.alert-danger { background: #fee2e2; color: #991b1b; }

/* Page loader */
.page-loader {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(243, 244, 248, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.12s ease;
}
.page-loader.visible { opacity: 1; pointer-events: all; }
.loader-bar {
    position: absolute;
    top: 0; left: 0;
    height: 3px;
    width: 0;
    background: var(--primary);
}
.page-loader.visible .loader-bar { animation: loader-progress 8s cubic-bezier(0.1, 0.6, 0.2, 1) forwards; }
.loader-spinner {
    width: 38px; height: 38px;
    border: 4px solid #c7d2fe;
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: loader-spin 0.8s linear infinite;
}
@keyframes loader-spin { to { transform: rotate(360deg); } }
@keyframes loader-progress { to { width: 90%; } }

/* Auth */
.auth-page .container {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    align-items: center;
    padding-top: calc(16px + env(safe-area-inset-top));
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
}
.auth-card {
    width: 100%;
    background: var(--card);
    border-radius: 20px;
    padding: 28px 22px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
}
.auth-card h1 { text-align: center; }
.auth-card .muted { text-align: center; margin: 4px 0 22px; }
.auth-logo {
    width: 60px; height: 60px; margin: 0 auto 12px;
    display: flex; align-items: center; justify-content: center;
    background: var(--primary); color: #fff; border-radius: 18px; font-size: 1.8rem;
}
.auth-switch { text-align: center; margin: 18px 0 0; color: var(--muted); }
.auth-switch a { color: var(--primary); font-weight: 600; }
