:root {
    --ink: #1f2933;
    --muted: #64748b;
    --line: #d8dee8;
    --panel: #ffffff;
    --soft: #eef6f8;
    --primary: #0f6b7a;
    --primary-dark: #094856;
    --accent: #b7791f;
    --danger: #a33a2a;
    --success: #1f7a4d;
    --success-soft: #eaf7ef;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: #f4f7fa; color: var(--ink); }
a { color: var(--primary); text-decoration: none; }
h1, h2, h3, p { margin-top: 0; }
.app-frame { display: grid; grid-template-columns: 250px minmax(0, 1fr); min-height: 100vh; }
.sidebar { background: #102a33; color: #fff; padding: 22px; display: flex; flex-direction: column; gap: 22px; }
.brand { color: #fff; font-weight: 800; font-size: 20px; display: flex; gap: 10px; align-items: center; }
.brand-mark { width: 34px; height: 34px; border-radius: 8px; display: inline-grid; place-items: center; background: var(--primary); color: #fff; font-weight: 900; }
.nav-list { display: grid; gap: 6px; }
.nav-list a { color: #dbeafe; padding: 10px 12px; border-radius: 8px; }
.nav-list a:hover { background: rgba(255,255,255,.1); }
.app-version { margin-top: auto; color: #aac6cf; font-size: 13px; }
.topbar { height: 70px; display: flex; justify-content: space-between; align-items: center; padding: 0 28px; background: #fff; border-bottom: 1px solid var(--line); }
.topbar span { display: block; color: var(--muted); font-size: 13px; text-transform: capitalize; }
.content { padding: 28px; }
.page-heading, .record-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 18px; margin-bottom: 22px; }
.page-heading p, .record-header p, .muted { color: var(--muted); }
.btn { border: 1px solid var(--line); background: #fff; color: var(--ink); border-radius: 8px; padding: 10px 14px; font-weight: 800; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; min-height: 40px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.ghost { background: transparent; }
.grid, .metric-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 14px; }
/* Utility column-count modifiers for the several places a view needs a 2- or
   3-column .grid rather than the 5-column default - a class here (unlike an
   inline grid-template-columns on the element) still yields to the mobile
   breakpoint below, so these actually collapse to one column on a phone. */
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.card, .metric, .panel, .auth-panel { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 18px; }
.metric span { color: var(--muted); display: block; }
.metric strong { font-size: 26px; display: block; margin-top: 8px; }
.table-wrap { overflow-x: auto; background: #fff; border: 1px solid var(--line); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; min-width: 760px; }
th, td { padding: 13px 14px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .04em; }
.pill, .tag-pill { display: inline-flex; align-items: center; min-height: 26px; border-radius: 999px; background: var(--soft); color: var(--primary-dark); padding: 0 10px; font-size: 13px; font-weight: 800; }
/* A tappable .pill (the "recent categories" quick-pick chips) - button-reset
   on top of the pill look, with a taller hit area than a static pill needs. */
.chip { border: 1px solid var(--line); cursor: pointer; min-height: 36px; padding: 0 14px; font: inherit; font-size: 13px; }
.chip:active { background: var(--primary); color: #fff; border-color: var(--primary); }
.workspace-grid { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 18px; align-items: start; }
.workspace-main, .workspace-side { background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 18px; }
.workspace-side { display: grid; gap: 14px; background: transparent; border: 0; padding: 0; }
.side-form, .section { background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 16px; }
.section-heading { display: flex; justify-content: space-between; gap: 12px; margin: 22px 0 10px; align-items: center; }
.section-heading:first-child { margin-top: 0; }
.record-editor, .data-form { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 18px; margin-bottom: 20px; }
label { display: grid; gap: 7px; font-weight: 800; font-size: 14px; }
/* font-size fixed at 16px (not just inherited) so a focused field never
   drops below iOS Safari's auto-zoom threshold - any inline style on a
   specific input that overrides this back down is a bug, not a variant. */
input, select, textarea { width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 10px 11px; font: inherit; font-size: 16px; background: #fff; }
textarea { resize: vertical; }
.wide { grid-column: 1 / -1; }
.form-actions { grid-column: 1 / -1; display: flex; justify-content: flex-end; gap: 10px; }
.list { display: grid; gap: 8px; }
.row-card { border: 1px solid var(--line); border-radius: 8px; padding: 12px; background: #fff; }
.row-card { position: relative; padding-right: 96px; }
.row-card span { color: var(--muted); display: block; font-size: 13px; margin-top: 4px; }
.icon-btn { width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--line); background: #fff; color: var(--primary); font-weight: 900; cursor: pointer; display: inline-grid; place-items: center; }
.row-actions { position: absolute; top: 10px; right: 10px; display: flex; gap: 6px; }
.row-actions .icon-btn { position: static; }
.tabs { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0 14px; }
.tabs a { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; background: #fff; color: var(--ink); font-weight: 800; }
.tab-panel { display: none; background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 18px; }
.tab-panel:target { display: block; }
.tab-panel:first-of-type { display: block; }
.tabs:has(+ #tab-details:target) ~ .tab-panel:first-of-type,
.tabs:has(~ #tab-comms:target) ~ #tab-details,
.tabs:has(~ #tab-addresses:target) ~ #tab-details,
.tabs:has(~ #tab-relationships:target) ~ #tab-details,
.tabs:has(~ #tab-activities:target) ~ #tab-details,
.tabs:has(~ #tab-notes:target) ~ #tab-details,
.tabs:has(~ #tab-audit:target) ~ #tab-details { display: none; }
.compact { margin-bottom: 0; }
dialog.modal { border: 1px solid var(--line); border-radius: 8px; width: min(620px, calc(100vw - 32px)); max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); overflow-y: auto; padding: 20px; }
dialog.modal::backdrop { background: rgba(16,42,51,.35); }
.auth-page {
    min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; background: #e8f2f4;
    padding: max(24px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(24px, env(safe-area-inset-left));
}
.auth-shell { width: min(430px, 100%); }
.form-stack { display: grid; gap: 14px; }
/* A long unbroken token (2FA manual setup key, say) would otherwise force
   the panel wider than the viewport instead of wrapping. */
.auth-panel, .card, .panel { overflow-wrap: anywhere; }
/* The QR image is a raster with a fixed pixel size baked in by the
   generator library - without this it doesn't shrink for a narrow phone
   and pushes the auth card wider than the viewport. */
[data-qr-code] img { max-width: 100%; height: auto; }
.notice { background: #e8f5ee; color: #226344; padding: 12px; border-radius: 8px; margin-bottom: 14px; }
.alert { background: #fdeeee; color: var(--danger); padding: 12px; border-radius: 8px; margin-bottom: 14px; }
.banner { position: relative; padding-right: 36px; }
.banner-close { position: absolute; top: 8px; right: 8px; background: transparent; border: none; font-size: 18px; line-height: 1; cursor: pointer; color: inherit; opacity: 0.6; width: 24px; height: 24px; display: grid; place-items: center; padding: 0; }
.banner-close:hover { opacity: 1; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.pipeline-board { display: grid; grid-template-columns: repeat(4, minmax(240px, 1fr)); gap: 14px; overflow-x: auto; }
.pipeline-column { background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 12px; min-height: 240px; }
.pipeline-column h2 { font-size: 16px; }
.autosave-status { grid-column: 1 / -1; color: var(--muted); min-height: 20px; font-weight: 800; }
.autosave-status[data-state="saved"] { color: #226344; }
.autosave-status[data-state="error"] { color: var(--danger); }
.account-section { margin-bottom: 26px; }
.account-section h2 { font-size: 16px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 10px; }
.account-section-heading { display: flex; justify-content: space-between; align-items: baseline; }
.account-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.account-card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 14px 16px; cursor: pointer; user-select: none; }
.account-card:active { background: var(--soft); }
.account-card-institution { color: var(--muted); font-size: 13px; }
.account-card-name { font-weight: 800; margin: 2px 0 10px; }
.account-card-balance { font-size: 20px; font-weight: 800; }
.account-card-type { color: var(--muted); font-size: 13px; margin-top: 4px; }

.txn-list { display: grid; gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.txn-row { background: #fff; padding: 12px 14px; display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.txn-row-main { min-width: 0; }
.txn-row-desc { font-weight: 700; overflow-wrap: anywhere; }
.txn-row-meta { color: var(--muted); font-size: 13px; margin-top: 2px; }
.txn-row-amount { font-weight: 800; white-space: nowrap; }
.txn-row-amount.negative { color: var(--danger); }
.budget-heading-row td { background: var(--soft); font-weight: 800; }
.budget-heading-row td:first-child { font-size: 15px; }
.txn-row-categorised { background: var(--success-soft); }
.categorised-badge { color: var(--success); font-weight: 800; }
.tag-pill form { display: inline-flex; margin-left: 4px; }
/* The pill itself stays compact, but its own remove button gets a real hit
   area via padding rather than sizing to the glyph - keeps the dense inline
   layout (many pills per line) while still giving the "x" more than a few
   px to tap. */
.tag-pill button { background: none; border: none; color: inherit; cursor: pointer; font-weight: 900; padding: 6px 4px; margin: -6px -4px; line-height: 1; }

.page-heading-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 22px; }

@media (max-width: 900px) {
    .app-frame, .workspace-grid { grid-template-columns: 1fr; }
    .sidebar { position: static; }
    .grid, .metric-grid, .record-editor, .data-form, .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
    .app-frame { display: block; }
    .sidebar {
        flex-direction: row; align-items: center; padding: 12px 16px; gap: 14px;
        position: sticky; top: 0; z-index: 10;
        /* Sticky-at-top bar, so it's the one thing that can sit under a
           notch/Dynamic Island in landscape or when Safari's own chrome is
           collapsed - pad the safe area instead of letting content run
           under it. viewport-fit=cover (see the layout's <meta viewport>)
           is what makes these env() values non-zero in the first place. */
        padding-top: max(12px, env(safe-area-inset-top));
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
    }
    .sidebar .brand span:last-child { display: none; }
    .nav-list { display: flex; flex-direction: row; gap: 4px; flex: 1; overflow-x: auto; -webkit-overflow-scrolling: touch; }
    /* min-height 44px (not just padding) meets Apple's touch-target
       guidance regardless of how the 14px label happens to measure. */
    .nav-list a { display: inline-flex; align-items: center; min-height: 44px; padding: 8px 12px; white-space: nowrap; font-size: 14px; }
    .app-version { display: none; }
    .topbar { height: auto; padding: 12px 16px; flex-wrap: wrap; gap: 8px; }
    .content { padding: 14px; padding-bottom: max(14px, env(safe-area-inset-bottom)); }
    .record-editor, .data-form { grid-template-columns: 1fr; padding: 14px; }
    table { min-width: 0; }
    .page-heading, .record-header { flex-direction: column; }
    /* .icon-btn (row-card delete/remove buttons) and .btn both sit under
       Apple's 44pt minimum touch target at their desktop size - only worth
       the extra visual weight of a bigger hit area once there's no mouse
       pointer to rely on. */
    .icon-btn { width: 44px; height: 44px; }
    .btn { min-height: 44px; }

    /* Opt-in per-table: a wide multi-column table (the budget list is the
       first case) genuinely can't fit a phone width even scrolling - turn
       each row into a stacked card instead. td[data-label] gets its label
       from CSS (attr()) rather than duplicating the header text in markup;
       a td with no data-label (the category name, the edit/delete details)
       just stacks full-width with no label prefix. */
    .responsive-cards table, .responsive-cards tbody, .responsive-cards tr, .responsive-cards td { display: block; }
    .responsive-cards thead { display: none; }
    .responsive-cards tr { border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px; margin-bottom: 10px; }
    .responsive-cards tr:last-child { margin-bottom: 0; }
    .responsive-cards td { border-bottom: none; padding: 4px 0; }
    .responsive-cards td[data-label] { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
    .responsive-cards td[data-label]::before {
        content: attr(data-label); color: var(--muted); font-size: 12px; font-weight: 800;
        text-transform: uppercase; letter-spacing: .04em; white-space: nowrap;
    }
    .responsive-cards td:first-child {
        font-weight: 800; font-size: 15px; padding: 0 0 8px; margin-bottom: 6px; border-bottom: 1px solid var(--line);
    }
    .responsive-cards tr.budget-heading-row { background: var(--soft); }
    .responsive-cards tr.budget-heading-row td { background: transparent; }
}
