/* MoneyTrek — visual language inherited from BookTrek's redesign: neutral
   page canvas, white cards, one restrained accent reserved for active states
   and primary actions. MoneyTrek's accent is a moss teal (cairn stone and
   moss) so the two apps read as siblings, not the same app. */

:root {
    color-scheme: light;
    --bg: #f8f9fa;
    --bg-gradient: none;
    --card: #ffffff;
    --text: #0f172a;
    --muted: #64748b;
    --border: #e2e8f0;
    --border-soft: #f1f5f9;
    --accent: #0f766e;
    --accent-dark: #0c5f59;
    --accent-soft: #e4f2f0;
    --sub: #475569;

    --ok-bg: #d1fae5;      --ok-fg: #047857;
    --warn-bg: #fef3c7;    --warn-fg: #b45309;
    --danger-fg: #b3261e;

    --up: #047857;
    --down: #b3261e;

    /* Chart series: 1 = pensions, 2 = savings & shares */
    --series-1: #0f766e;
    --series-2: #b45309;

    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
    --shadow-md: 0 4px 14px rgba(15, 23, 42, 0.06);

    --content-max: 1080px;
}

@media (prefers-color-scheme: dark) {
    :root {
        color-scheme: dark;
        --bg: #151719;
        --bg-gradient: radial-gradient(circle at 15% 0%, #1d2427 0%, #151719 40%);
        --card: #22282b;
        --text: #edf1ef;
        --muted: #93a09b;
        --border: #3b4649;
        --border-soft: #2b3336;
        --accent: #35b8ac;
        --accent-dark: #52cabf;
        --accent-soft: #143a37;
        --sub: #b4c0bb;
        --ok-bg: #064e3b;   --ok-fg: #6ee7b7;
        --warn-bg: #4a3208; --warn-fg: #fcd34d;
        --danger-fg: #f2a8a2;
        --up: #6ee7b7;
        --down: #f2a8a2;
        --series-1: #35b8ac;
        --series-2: #e8a04c;
        --shadow-sm: none;
        --shadow-md: none;
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    background-image: var(--bg-gradient);
    background-attachment: fixed;
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

main {
    max-width: var(--content-max);
    margin: 0 auto;
    padding: calc(3.6rem + env(safe-area-inset-top)) 1rem calc(5.5rem + env(safe-area-inset-bottom));
}

h1 { font-size: 1.45rem; font-weight: 800; letter-spacing: -0.02em; margin: 0.8rem 0 0.15rem; }
h2 { font-size: 1.02rem; font-weight: 700; letter-spacing: -0.01em; margin: 0 0 0.6rem; }

.page-sub { color: var(--sub); font-size: 0.92rem; margin: 0 0 1.1rem; }

/* ---------------------------------------------------------------- topbar */

.topbar {
    position: fixed;
    top: 0; left: 0; right: 0;
    z-index: 30;
    background: var(--card);
    border-bottom: 1px solid var(--border-soft);
    padding-top: env(safe-area-inset-top);
}
.topbar-inner {
    max-width: var(--content-max);
    margin: 0 auto;
    padding: 0.55rem 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.wordmark-btn { background: none; border: 0; padding: 0; cursor: pointer; }
.wordmark {
    display: inline-flex;
    align-items: center;
    gap: 0.15rem;
    font-family: inherit;
    font-size: 1.15rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    color: var(--text);
}
.wordmark-trek { color: var(--accent); }
.wordmark-cairn {
    height: 1.1em; width: 1.1em;
    margin-left: 0.22rem;
    fill: var(--accent);
    opacity: 0.9;
}
.topbar-signout {
    background: none;
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--muted);
    font: inherit;
    font-size: 0.8rem;
    font-weight: 600;
    padding: 0.3rem 0.8rem;
    cursor: pointer;
}
.topbar-signout:hover { color: var(--text); border-color: var(--muted); }

/* ---------------------------------------------------------------- tab nav */

.tab-nav {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    z-index: 30;
    display: flex;
    justify-content: space-around;
    max-width: var(--content-max);
    margin: 0 auto;
    background: var(--card);
    border-top: 1px solid var(--border-soft);
    padding: 0.35rem 0 calc(0.35rem + env(safe-area-inset-bottom));
}
.nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.1rem;
    flex: 1;
    padding: 0.25rem 0;
    text-decoration: none;
    color: var(--muted);
    font-size: 0.68rem;
    font-weight: 600;
}
.nav-item .nav-ic svg { width: 1.35rem; height: 1.35rem; display: block; }
.nav-item.on { color: var(--accent); }

/* ---------------------------------------------------------------- cards */

.card {
    background: var(--card);
    border: 1px solid var(--border-soft);
    border-radius: 16px;
    box-shadow: var(--shadow-sm);
    padding: 1rem 1.1rem;
    margin-bottom: 0.9rem;
}

.card-footnote { color: var(--muted); font-size: 0.82rem; margin: 0.7rem 0 0; line-height: 1.5; }
.card-footnote a { color: var(--accent); font-weight: 600; text-decoration: none; }

/* ---------------------------------------------------------------- dashboard */

.stat-hero { display: flex; flex-direction: column; gap: 0.15rem; }
.stat-label {
    color: var(--muted);
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.stat-value { font-size: 2rem; font-weight: 800; letter-spacing: -0.03em; }
.stat-delta { font-size: 0.85rem; font-weight: 600; }
.stat-delta.up { color: var(--up); }
.stat-delta.down { color: var(--down); }

.stat-row, .summary-strip {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.6rem;
    margin-bottom: 0.9rem;
}
.stat-mini { margin: 0; padding: 0.75rem 0.9rem; display: flex; flex-direction: column; gap: 0.15rem; }
/* Bottom-pinned so the figures stay on one line with each other even when
   a neighbour's label wraps on a narrow screen. */
.stat-mini-value { font-size: 1.05rem; font-weight: 700; letter-spacing: -0.01em; margin-top: auto; }
.stat-mini-next { color: var(--muted); font-size: 0.7rem; font-weight: 600; }
.stat-mini-next.neg { color: var(--danger-fg); }
.stat-mini-value.neg, .amount.neg { color: var(--danger-fg); }

.goal-head { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; gap: 0.6rem; }
.goal-edit[open] { flex-basis: 100%; }
.goal-edit summary {
    color: var(--muted);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    list-style: none;
}
.goal-edit summary::-webkit-details-marker { display: none; }
.goal-bar {
    height: 12px;
    border-radius: 999px;
    background: var(--border-soft);
    overflow: hidden;
    margin: 0.55rem 0 0.6rem;
}
.goal-fill {
    height: 100%;
    border-radius: 999px;
    background: linear-gradient(90deg, var(--accent), var(--accent-dark));
    min-width: 6px;
}
.goal-figures {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem 1.1rem;
    color: var(--sub);
    font-size: 0.86rem;
}
.goal-figures strong { color: var(--text); }
.goal-reached { color: var(--ok-fg); font-weight: 700; }

.chart-card h2 { margin-bottom: 0.3rem; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 0.4rem 1.1rem; margin-bottom: 0.4rem; }
.legend-item { display: inline-flex; align-items: center; gap: 0.4rem; font-size: 0.8rem; color: var(--sub); font-weight: 600; }
.legend-swatch { width: 0.85rem; height: 0.3rem; border-radius: 2px; display: inline-block; }
.legend-1 .legend-swatch { background: var(--series-1); }
.legend-2 .legend-swatch { background: var(--series-2); }
.chart-scroll { overflow-x: auto; }
.trend-chart { width: 100%; min-width: 480px; height: auto; display: block; }
.grid-line { stroke: var(--border-soft); stroke-width: 1; }
.axis-line { stroke: var(--border); stroke-width: 1; }
.axis-label { fill: var(--muted); font-size: 11px; font-family: inherit; }
.trend-line { fill: none; stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.trend-line.trend-1 { stroke: var(--series-1); }
.trend-line.trend-2 { stroke: var(--series-2); }
.trend-dot.trend-1 { fill: var(--series-1); }
.trend-dot.trend-2 { fill: var(--series-2); }

.left-rows { display: flex; flex-direction: column; }
.left-row {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0;
    border-bottom: 1px solid var(--border-soft);
    font-size: 0.95rem;
}
.left-row:last-child { border-bottom: 0; }
.left-total { font-weight: 700; }
.left-row .amount { font-variant-numeric: tabular-nums; font-weight: 600; }

/* ---------------------------------------------------------------- accounts */

.total-strip {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 0.8rem 1.1rem;
    font-size: 0.95rem;
}
.total-strip strong { font-size: 1.15rem; letter-spacing: -0.01em; }

.account-group { margin-bottom: 1.4rem; }
.group-head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 0 0.2rem;
    margin-bottom: 0.5rem;
}
.group-head h2 { margin: 0; }
.group-total { color: var(--sub); font-weight: 700; font-size: 0.92rem; font-variant-numeric: tabular-nums; }

.subgroup-head {
    color: var(--muted);
    font-size: 0.74rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin: 0.6rem 0.2rem 0.45rem;
}
.account-card { padding: 0.85rem 1rem; margin-bottom: 0.55rem; }
.account-main { display: flex; justify-content: space-between; align-items: center; gap: 0.8rem; }
.account-names { display: flex; flex-direction: column; gap: 0.1rem; min-width: 0; }
.account-name { font-weight: 650; font-size: 0.95rem; }
.account-meta { color: var(--muted); font-size: 0.78rem; }
.account-balance { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
/* Tap the account card to log a balance (the monthly habit); editing the
   account's details is a second, quieter step inside. */
details.account-card { display: block; }
.account-card.row-tap[open] > summary .account-name { color: var(--accent); }
.edit-more { margin-top: 0.3rem; }
.edit-more > summary { font-size: 0.8rem; }

/* ---------------------------------------------------------------- bills */

.section-label {
    color: var(--muted);
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin: 1.4rem 0.2rem 0.6rem;
}

.due-badge {
    background: var(--border-soft);
    color: var(--sub);
    font-size: 0.72rem;
    font-weight: 650;
    padding: 0.18rem 0.55rem;
    border-radius: 999px;
    white-space: nowrap;
}
.due-badge.soon { background: var(--warn-bg); color: var(--warn-fg); }
.due-badge.overdue { background: var(--warn-bg); color: var(--danger-fg); }
.offer-star { color: #d9a441; font-size: 0.85em; }

.bill-group { padding-bottom: 0.55rem; scroll-margin-top: 3.6rem; /* anchored sort links land clear of the fixed topbar */ }
/* Card payment rows: hero due-amount, then the due chip, then a muted
   liability line — cash-out-now and total-debt no longer compete. */
.pay-row { padding: 0.6rem 0; border-top: 1px solid var(--border-soft); }
.pay-row:first-of-type { border-top: 0; }
.pay-top { display: flex; align-items: baseline; flex-wrap: wrap; gap: 0.6rem; }
.pay-name { flex: 1 1 auto; min-width: 8rem; font-weight: 650; font-size: 0.95rem; }
.pay-due { font-weight: 750; font-size: 1.08rem; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.pay-liability { color: var(--muted); font-size: 0.76rem; }
div.pay-liability { margin-top: 0.15rem; }
.pay-liability strong { color: var(--sub); font-weight: 600; }

/* Outflow vs liability vs forecast, as three aligned figures */
.stat-footer {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
    border-top: 1px solid var(--border-soft);
    margin-top: 0.7rem;
    padding-top: 0.7rem;
}
.stat-footer div { display: flex; flex-direction: column; gap: 0.1rem; }
/* Anchor the outer columns to the card edges (amounts above are
   right-aligned, so the last column meets them) and centre the middle. */
.stat-footer div:nth-child(2) { align-items: center; text-align: center; }
.stat-footer div:nth-child(3) { align-items: flex-end; text-align: right; }
.sf-label {
    color: var(--muted);
    font-size: 0.66rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.sf-value { font-size: 0.9rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.provider-head {
    color: var(--muted);
    font-size: 0.74rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-top: 0.55rem;
}
.provider-head + .line-row { border-top: 0; }
.provider-head { scroll-margin-top: 3.6rem; }
.sort-row + .line-row { border-top: 0; }
.bill-group .group-head { margin-bottom: 0.2rem; padding: 0; }
.income-card .line-row:first-of-type { border-top: 0; }

/* Two-line rows: name + amount on top, meta + due badge underneath.
   The one-flex-row-with-everything version crushed the name column into a
   sliver on phones. */
.line-row {
    padding: 0.55rem 0;
    border-top: 1px solid var(--border-soft);
}
.line-top { display: flex; align-items: center; flex-wrap: wrap; gap: 0.6rem; }
.line-top .line-name { flex: 1 1 auto; min-width: 8rem; }
.line-sub {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.25rem 0.6rem;
    margin-top: 0.15rem;
}
.line-sub .line-meta, .line-sub .pay-liability { flex: 1 1 auto; }
/* Due chips sit on the RIGHT in every section — dates line up under the
   amounts (and stay right-aligned even when a long meta line wraps). */
.line-sub .due-badge { margin-left: auto; }
/* An open edit expander takes the full width UNDER the row instead of
   squeezing into the top line. */
.line-edit[open] { flex-basis: 100%; }
.line-name { font-weight: 600; font-size: 0.92rem; }
.line-meta { color: var(--muted); font-size: 0.76rem; }
.line-amount { font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }
.line-amount small { color: var(--muted); font-weight: 500; }

/* Whole-row tap targets: the row itself is the <summary>, tapping it opens
   the edit form beneath (replaced the little ✎ pencils, which were fiddly
   on a phone). */
details.line-row, details.pay-row { display: block; }
.row-tap > summary { list-style: none; cursor: pointer; position: relative; padding-left: 0.75rem; }
/* Colour bar per item: category-coded for bills, section-coded elsewhere. */
.row-tap > summary::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0.35rem;
    bottom: 0.35rem;
    width: 3px;
    border-radius: 3px;
    background: var(--bar, var(--border));
}
.bar-income { --bar: var(--accent); }
.bar-card { --bar: #e58a1a; }
.bar-instalment { --bar: #9c6bce; }
.bar-bnpl { --bar: #d9679e; }
.bar-housing { --bar: #2e7dbb; }
.bar-entertainment { --bar: #8a7cc4; }
.bar-health { --bar: #3f9d68; }
.bar-insurance { --bar: #d9a441; }
.bar-phone-internet { --bar: #c4694f; }
.bar-loan { --bar: #b3261e; }
.bar-pension { --bar: var(--accent); }
.bar-cash { --bar: #3f9d68; }
.bar-cash_isa { --bar: #2e7dbb; }
.bar-shares { --bar: #e58a1a; }
.bar-isa { --bar: #8a7cc4; }
.row-tap > summary::-webkit-details-marker { display: none; }
.row-tap > summary:active { opacity: 0.7; }
.row-tap[open] > summary .line-name,
.row-tap[open] > summary .pay-name { color: var(--accent); }

/* ---------------------------------------------------------------- forms */

.inline-form {
    display: flex;
    flex-wrap: wrap;
    gap: 0.6rem;
    align-items: flex-end;
    padding: 0.7rem 0 0.3rem;
}
.inline-form label {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--muted);
}
.inline-form input, .inline-form select,
.auth-card input {
    font: inherit;
    font-size: 0.9rem;
    color: var(--text);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 9px;
    padding: 0.45rem 0.6rem;
    min-width: 0;
}
.inline-form input { width: 9.5rem; }

/* Money inputs show a £ inside the box — display only, you just type the
   number (the backend strips a typed £ anyway). */
.money { position: relative; display: inline-block; }
.money::before {
    content: '£';
    position: absolute;
    left: 0.6rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--muted);
    font-size: 0.9rem;
    pointer-events: none;
}
.money input { padding-left: 1.45rem; width: 100%; }
.inline-form .money { width: 9.5rem; }

/* Same iOS quirk BookTrek hit: a native date input keeps its own much
   taller intrinsic rendering (and centres its text) until appearance is
   stripped — then the explicit height matches its sibling fields. */
.inline-form input[type="date"], .auth-card input[type="date"] {
    -webkit-appearance: none;
    appearance: none;
    height: 2.35rem;
    line-height: 2.35rem;
    padding-top: 0;
    padding-bottom: 0;
    text-align: left;
    overflow: hidden;
}
input[type="date"]::-webkit-date-and-time-value { text-align: left; }

.sort-row {
    text-align: right;
    color: var(--muted);
    font-size: 0.72rem;
    font-weight: 600;
    margin: -0.1rem 0 0.3rem;
}
.sort-row a { color: var(--muted); text-decoration: none; font-weight: 700; padding: 0.15rem; }
.sort-row a.on { color: var(--accent); }
.inline-form input:focus, .inline-form select:focus, .auth-card input:focus {
    outline: 2px solid var(--accent);
    outline-offset: 0;
    border-color: transparent;
}

.btn-primary {
    background: var(--accent);
    color: #fff;
    border: 0;
    border-radius: 10px;
    font: inherit;
    font-weight: 700;
    padding: 0.55rem 1.2rem;
    cursor: pointer;
}
.btn-primary:hover { background: var(--accent-dark); }
.btn-sm { padding: 0.45rem 0.9rem; font-size: 0.85rem; }
.btn-ghost {
    display: inline-block;
    color: var(--accent);
    font-size: 0.84rem;
    font-weight: 650;
    cursor: pointer;
    list-style: none;
    padding: 0.2rem 0;
}
.btn-ghost::-webkit-details-marker { display: none; }
.btn-cancel {
    background: none;
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--muted);
    font: inherit;
    font-size: 0.85rem;
    font-weight: 600;
    padding: 0.45rem 0.9rem;
    cursor: pointer;
}
.btn-cancel:hover { color: var(--text); border-color: var(--muted); }
.btn-danger-ghost {
    background: none;
    border: 0;
    color: var(--danger-fg);
    font: inherit;
    font-size: 0.8rem;
    font-weight: 600;
    padding: 0.3rem 0;
    cursor: pointer;
}
.add-card details > summary { text-align: center; }
.add-inline { margin-top: 0.4rem; }

/* ---------------------------------------------------------------- auth */

.auth-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
}
.auth-card { width: 100%; max-width: 26rem; }
.auth-card label { display: block; font-size: 0.8rem; font-weight: 600; color: var(--muted); margin: 0.8rem 0 0.25rem; }
.auth-card input { width: 100%; }
.auth-title { margin-bottom: 0.3rem; }
.auth-title .wordmark { font-size: 1.4rem; }
.auth-subtitle { color: var(--muted); font-size: 0.9rem; margin: 0 0 1rem; }
.auth-subtitle.small { font-size: 0.8rem; margin: 0.4rem 0 0; line-height: 1.45; }
.auth-error {
    background: #fdecea;
    color: #b3261e;
    border: 1px solid #f4c7c3;
    border-radius: 9px;
    padding: 0.65rem 0.9rem;
    font-size: 0.88rem;
    margin-bottom: 1rem;
}
@media (prefers-color-scheme: dark) {
    .auth-error { background: #3a2422; color: #f2a8a2; border-color: #5c332f; }
}
.check-label {
    display: flex !important;
    flex-direction: row !important;
    align-items: center;
    gap: 0.5rem;
    margin-top: 1rem !important;
    font-weight: 500 !important;
    font-size: 0.88rem !important;
    color: var(--text) !important;
}
.check-label input { width: auto; }
.submit-row { margin-top: 1.2rem; }
.submit-row .btn-primary { width: 100%; }
.totp-qr { display: block; margin: 0 auto 0.6rem; border-radius: 12px; max-width: 200px; width: 100%; background: #fff; padding: 8px; }
code { background: var(--border-soft); border-radius: 6px; padding: 0.1rem 0.35rem; font-size: 0.85em; word-break: break-all; }

/* ---------------------------------------------------------------- narrow screens */

@media (max-width: 640px) {
    /* Phone: form fields stack full-width instead of wrapping at a fixed
       9.5rem, which overflowed the viewport inside cards. */
    .inline-form { flex-direction: column; align-items: stretch; }
    .inline-form label { width: 100%; }
    .inline-form input, .inline-form select { width: 100%; }
    .inline-form .money { width: 100%; }
    .inline-form .btn-primary { width: 100%; }
}

/* ---------------------------------------------------------------- wide screens */

@media (min-width: 760px) {
    .hero-stats { display: grid; grid-template-columns: 1.2fr 1fr; gap: 0.9rem; align-items: stretch; }
    .hero-stats .stat-hero { margin-bottom: 0.9rem; justify-content: center; }
    .stat-row { grid-template-columns: 1fr; margin-bottom: 0.9rem; }
    .stat-row .stat-mini { flex-direction: row; justify-content: space-between; align-items: center; }
}
