/* ============================================================
   Budgetify design system — "ledger noir"
   Dark-first fintech: layered ink surfaces, violet signal,
   mono numerals. Light theme mirrors the same scale.
   Token names are stable — scoped page CSS depends on them.
   ============================================================ */

:root[data-theme="dark"] {
  --bg-0:#0a0c13; --bg-1:#11141e; --bg-2:#171b28; --bg-3:#202637;
  --fg-0:#f1f2f9; --fg-1:#a6abc2; --fg-2:#6f7489;
  --rule:rgba(165,175,220,.10); --rule-strong:rgba(165,175,220,.18);
  --signal:#7c5cff; --signal-2:#9d7bff; --on-signal:#ffffff;
  --info:#5fdcef; --ok:#46e0a0; --warn:#ffb547; --err:#ff5e7e;
  --card:linear-gradient(180deg, rgba(165,175,220,.045), rgba(165,175,220,.015) 42%, transparent);
  --shadow-1:0 1px 2px rgba(2,4,10,.5), 0 8px 24px -12px rgba(2,4,10,.5);
  --shadow-2:0 24px 64px -24px rgba(2,4,10,.75);
  --glow:0 10px 30px -6px rgba(124,92,255,.55);
  --scrim:rgba(4,6,12,.62);
  --ease:cubic-bezier(.2,.7,.2,1);
  --font-display:'Bricolage Grotesque',system-ui,-apple-system,sans-serif;
  --font-sans:'Inter Tight',system-ui,-apple-system,'Segoe UI',Roboto,sans-serif;
  --font-mono:'Geist Mono','JetBrains Mono',ui-monospace,Menlo,Consolas,monospace;
  --wash:.9;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg-0:#eef0f6; --bg-1:#ffffff; --bg-2:#f7f8fc; --bg-3:#e9ebf4;
  --fg-0:#10131d; --fg-1:#414659; --fg-2:#6b7088;
  --rule:rgba(22,28,52,.10); --rule-strong:rgba(22,28,52,.18);
  --signal:#6a48e8; --signal-2:#8463ff; --on-signal:#ffffff;
  --info:#15829c; --ok:#1f9e60; --warn:#ad6e06; --err:#d63057;
  --card:linear-gradient(180deg, rgba(255,255,255,.85), rgba(255,255,255,.4));
  --shadow-1:0 1px 2px rgba(22,28,52,.06), 0 10px 28px -16px rgba(22,28,52,.18);
  --shadow-2:0 24px 64px -28px rgba(22,28,52,.3);
  --glow:0 10px 28px -8px rgba(106,72,232,.45);
  --scrim:rgba(18,22,40,.45);
  --ease:cubic-bezier(.2,.7,.2,1);
  --font-display:'Bricolage Grotesque',system-ui,-apple-system,sans-serif;
  --font-sans:'Inter Tight',system-ui,-apple-system,'Segoe UI',Roboto,sans-serif;
  --font-mono:'Geist Mono','JetBrains Mono',ui-monospace,Menlo,Consolas,monospace;
  --wash:.5;
  color-scheme: light;
}

/* ---------- base ---------- */
*,*::before,*::after { box-sizing: border-box; }
* { margin: 0; }
html,body { height: 100%; }
html { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
/* On touch devices, tapping/long-pressing UI shouldn't select text or flash a
   highlight (the app should feel native). Form fields stay selectable so users
   can still edit/copy values. Desktop keeps normal text selection. */
@media (pointer: coarse) {
  body { -webkit-user-select: none; user-select: none; }
  input, textarea, [contenteditable="true"] { -webkit-user-select: text; user-select: text; }
}
body {
  background: var(--bg-0); color: var(--fg-0); font-family: var(--font-sans);
  font-size: 14.5px; line-height: 1.5;
  transition: background-color .35s var(--ease), color .35s var(--ease);
  position: relative;
  -webkit-font-smoothing: antialiased;
}
/* Atmospheric violet + cyan wash behind everything. */
body::before {
  content: ""; position: fixed; inset: 0; z-index: -1; pointer-events: none; opacity: var(--wash);
  background:
    radial-gradient(1000px 640px at 85% -12%, rgba(124,92,255,.13), transparent 60%),
    radial-gradient(760px 560px at -4% 112%, rgba(95,220,239,.06), transparent 58%);
}
a { color: inherit; }
h1,h2,h3,h4 { font-family: var(--font-display); font-weight: 650; line-height: 1.1; letter-spacing: -.01em; }
button { font: inherit; }
::selection { background: color-mix(in srgb, var(--signal) 32%, transparent); }
:focus-visible { outline: 2px solid color-mix(in srgb, var(--signal) 75%, transparent); outline-offset: 2px; border-radius: 4px; }
/* FocusOnNavigate programmatically focuses the page heading — don't ring it. */
h1:focus-visible, h2:focus-visible, h3:focus-visible, [tabindex="-1"]:focus-visible { outline: none; }

/* Subtle, theme-aware scrollbars (WebKit). */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--rule-strong); border-radius: 8px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- shared primitives ---------- */
.muted { color: var(--fg-2); }

.theme-toggle {
  background: var(--bg-2); border: 1px solid var(--rule); border-radius: 10px;
  width: 38px; height: 38px; cursor: pointer; color: var(--fg-1); flex: none;
  transition: color .14s var(--ease), border-color .14s var(--ease);
}
.theme-toggle:hover { color: var(--fg-0); border-color: var(--rule-strong); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  text-decoration: none; appearance: none; cursor: pointer; white-space: nowrap;
  border: 1px solid var(--rule-strong); background: var(--bg-2); color: var(--fg-0);
  min-height: 38px; padding: 7px 16px; border-radius: 10px; font-weight: 500; font-size: 14px;
  transition: border-color .14s var(--ease), background .14s var(--ease),
              transform .14s var(--ease), box-shadow .14s var(--ease), filter .14s var(--ease);
}
.btn:hover { border-color: color-mix(in srgb, var(--fg-2) 60%, transparent); transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn.primary {
  background: linear-gradient(160deg, var(--signal-2), var(--signal) 60%);
  color: var(--on-signal); border-color: transparent; font-weight: 600; box-shadow: var(--glow);
}
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--err); border-color: color-mix(in srgb, var(--err) 45%, transparent); background: transparent; }
.btn.danger:hover { background: color-mix(in srgb, var(--err) 10%, transparent); border-color: var(--err); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--fg-1); }
.btn.ghost:hover { background: var(--bg-2); color: var(--fg-0); transform: none; }
.btn.sm { min-height: 30px; padding: 3px 10px; font-size: 13px; border-radius: 8px; }
.btn.lg { min-height: 46px; padding: 11px 24px; font-size: 15px; border-radius: 12px; }
.btn[disabled] { opacity: .55; pointer-events: none; }

.input,.select,textarea.input {
  background: var(--bg-1); border: 1px solid var(--rule-strong); color: var(--fg-0);
  min-height: 40px; padding: 8px 12px; border-radius: 10px; width: 100%;
  font-family: var(--font-sans); font-size: 14px;
  transition: border-color .14s var(--ease), box-shadow .14s var(--ease);
}
.input::placeholder { color: var(--fg-2); }
.input:focus,.select:focus {
  outline: none; border-color: var(--signal);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--signal) 22%, transparent);
}
.select { cursor: pointer; }

/* Password field with a show/hide toggle (window.budgetify.togglePw). */
.pwwrap { position: relative; display: flex; align-items: center; }
.pwwrap .input { padding-right: 44px; }
.pweye {
  position: absolute; right: 4px; display: grid; place-items: center;
  width: 36px; height: 32px; background: none; border: none; border-radius: 8px;
  color: var(--fg-2); cursor: pointer; font-size: 16px; line-height: 1;
}
.pweye:hover { color: var(--fg-0); }
input[type="date"].input, input[type="month"].input { font-family: var(--font-mono); font-size: 13px; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 12px; font-weight: 550; color: var(--fg-1); letter-spacing: .02em; }

/* Inline field validation (for the always-visible add forms). */
.field-err { color: var(--err); font-size: 12px; margin-top: 4px; }
.input.invalid, .select.invalid { border-color: var(--err); }

/* Card surface — the standard container for page sections. */
.card {
  background: var(--bg-1); background-image: var(--card);
  border: 1px solid var(--rule); border-radius: 14px;
  box-shadow: var(--shadow-1);
  padding: 18px;
}

/* Page header — kicker + display title + actions. */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 14px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head .kicker {
  font-family: var(--font-mono); font-size: 11px; text-transform: uppercase;
  letter-spacing: .14em; color: var(--fg-2); margin-bottom: 6px; display: flex; align-items: center; gap: 8px;
}
.page-head h1, .page-head h2 { font-size: 26px; }
.page-head .sub { color: var(--fg-2); margin-top: 6px; font-size: 13.5px; max-width: 64ch; }
.page-head .actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* Toolbar — a card strip holding filters / inline add forms. */
.toolbar {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  background: var(--bg-1); background-image: var(--card);
  border: 1px solid var(--rule); border-radius: 14px; box-shadow: var(--shadow-1);
  padding: 12px; margin-bottom: 18px;
}
.toolbar .input, .toolbar .select { width: auto; flex: 1 1 140px; }

/* Status badge */
.badge {
  font-size: 10px; font-family: var(--font-mono); text-transform: uppercase; letter-spacing: .08em;
  padding: 3px 8px; border-radius: 6px; background: var(--bg-3); color: var(--fg-1);
  display: inline-flex; align-items: center; gap: 5px; line-height: 1.2; white-space: nowrap;
}
.badge.ok { background: color-mix(in srgb, var(--ok) 14%, transparent); color: var(--ok); }
.badge.warn { background: color-mix(in srgb, var(--warn) 14%, transparent); color: var(--warn); }
.badge.err { background: color-mix(in srgb, var(--err) 14%, transparent); color: var(--err); }
.badge.signal { background: color-mix(in srgb, var(--signal) 16%, transparent); color: var(--signal-2); }

/* Numeric emphasis */
.num, .mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

/* Skeleton loading placeholders — reduce layout shift vs. a bare "Loading…". */
.skeleton { position: relative; overflow: hidden; background: var(--bg-2); border-radius: 8px; color: transparent !important; }
.skeleton::after { content: ""; position: absolute; inset: 0; transform: translateX(-100%);
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--fg-2) 14%, transparent), transparent);
  animation: skeleton-sweep 1.25s var(--ease) infinite; }
@keyframes skeleton-sweep { to { transform: translateX(100%); } }

/* ---------- app shell ---------- */
.shell { min-height: 100vh; min-height: 100dvh; }
.content { padding: 20px 16px; min-width: 0; max-width: 1180px; margin-inline: auto; width: 100%; animation: page-in .3s var(--ease) both; }
@keyframes page-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.errbox { max-width: 460px; margin: 48px auto; text-align: center; padding: 32px 26px; background: var(--bg-1); background-image: var(--card); border: 1px solid var(--rule); border-radius: 16px; color: var(--fg-1); box-shadow: var(--shadow-1); }
.errbox .btn { text-decoration: none; margin-top: 12px; }

/* Mobile chrome (top bar, bottom tab bar, sheets) is hidden until the breakpoint. */
.mtopbar, .bottomnav, .sheet { display: none; }
body.sheet-open { overflow: hidden; }

/* Desktop: fixed rail column + content. Mobile chrome stays hidden. */
@media (min-width: 821px) {
  .shell { display: grid; grid-template-columns: 232px 1fr; }
  .content { padding: 30px 36px 56px; }
}

/* Mobile: hide the desktop rail; show top bar + bottom tab bar + sheets.
   !important: the scoped LeftRail.razor.css bundle loads after this file and
   its `.rail { display:flex }` would otherwise win the cascade tie. */
@media (max-width: 820px) {
  .shell .rail { display: none !important; }
  .content { padding-bottom: calc(96px + env(safe-area-inset-bottom)); }

  .mtopbar {
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top));
    border-bottom: 1px solid var(--rule); position: sticky; top: 0; z-index: 30;
    background: color-mix(in srgb, var(--bg-0) 78%, transparent);
    -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  }
  .mtopbar .mbrand { font-family: var(--font-mono); font-weight: 700; letter-spacing: .05em; text-decoration: none; color: var(--fg-0); }
  .mtopbar .spaceswitch {
    display: flex; align-items: center; gap: 8px; max-width: 72vw; min-height: 42px;
    background: var(--bg-2); border: 1px solid var(--rule); border-radius: 11px;
    padding: 7px 12px; color: var(--fg-0); cursor: pointer; font-size: 14px;
  }
  .mtopbar .spaceswitch .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
  .mtopbar .spaceswitch .caret { opacity: .55; font-size: 11px; }

  .bottomnav {
    display: flex; align-items: stretch; position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
    background: color-mix(in srgb, var(--bg-0) 82%, transparent);
    -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
    border-top: 1px solid var(--rule); padding-bottom: env(safe-area-inset-bottom);
  }
  .bottomnav .bn {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 3px; min-height: 60px; padding: 8px 0 7px; background: none; border: none; cursor: pointer;
    color: var(--fg-2); text-decoration: none; font-size: 10px; letter-spacing: .02em; position: relative;
    transition: color .14s var(--ease);
  }
  .bottomnav .bn .ic { font-size: 19px; line-height: 1; }
  .bottomnav .bn.on, .bottomnav .bn[aria-current="page"] { color: var(--fg-0); }
  .bottomnav .bn.on::before, .bottomnav .bn[aria-current="page"]::before {
    content: ""; position: absolute; top: 0; left: 50%; transform: translateX(-50%);
    width: 28px; height: 2px; border-radius: 0 0 2px 2px; background: var(--signal);
  }
  .bottomnav .bn-gap { flex: 0 0 72px; }

  .sheet { display: block; position: fixed; inset: 0; z-index: 60; pointer-events: none; }
  .sheet .sheet-scrim { position: fixed; inset: 0; background: var(--scrim); opacity: 0; transition: opacity .2s var(--ease); }
  .sheet .sheet-panel {
    position: fixed; left: 0; right: 0; bottom: 0; max-height: 82vh; overflow-y: auto;
    transform: translateY(100%); transition: transform .26s var(--ease);
    background: var(--bg-1); border-top: 1px solid var(--rule-strong);
    border-top-left-radius: 20px; border-top-right-radius: 20px;
    padding: 8px 14px calc(16px + env(safe-area-inset-bottom));
    box-shadow: var(--shadow-2);
  }
  .sheet.open { pointer-events: auto; }
  .sheet.open .sheet-scrim { opacity: 1; }
  .sheet.open .sheet-panel { transform: translateY(0); }
  .sheet .sheet-grip { width: 38px; height: 4px; border-radius: 2px; background: var(--rule-strong); margin: 8px auto 12px; }
  .sheet .sheet-lbl { font-family: var(--font-mono); font-size: 10px; text-transform: uppercase; letter-spacing: .12em; color: var(--fg-2); padding: 12px 10px 5px; }
  .sheet .srow { display: flex; align-items: center; gap: 10px; min-height: 48px; padding: 11px 12px; border-radius: 11px; color: var(--fg-1); text-decoration: none; font-size: 15px; }
  .sheet .srow:active { background: var(--bg-2); }
  .sheet .srow.on { background: var(--bg-3); color: var(--fg-0); font-weight: 600; }
  .sheet .srow.new { opacity: .7; }
  .sheet .sheet-div { height: 1px; background: var(--rule); margin: 8px 4px; }

  /* space-type dots in the top bar + sheets (mirrors the rail's dot colors) */
  .spaceswitch .dot, .sheet .dot { width: 9px; height: 9px; border-radius: 3px; background: var(--fg-2); flex: none; }
  .spaceswitch .dot.personal, .sheet .dot.personal { background: var(--ok); }
  .spaceswitch .dot.household, .sheet .dot.household { background: var(--signal); }
  .spaceswitch .dot.external, .sheet .dot.external { background: var(--info); }
}

/* Larger touch targets on coarse pointers. */
@media (pointer: coarse) {
  .btn { min-height: 44px; }
  .btn.sm { min-height: 36px; }
  .input, .select { min-height: 44px; }
  .theme-toggle { width: 42px; height: 42px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* Print / Save-as-PDF: drop chrome, show only content */
@media print {
  .mtopbar, .bottomnav, .sheet { display: none !important; }
  .shell .rail { display: none !important; }
  .shell { display: block !important; }
  .content { padding: 0 !important; }
  body::before { display: none; }
}
