:root {
  --bg: #0f1420;
  --bg-soft: #161d2e;
  --card: #1b2436;
  --card-hover: #212c42;
  --border: #2a3550;
  --text: #eef1f8;
  --text-dim: #9aa4bd;
  --primary: #4f7cff;
  --primary-dim: #3a5cd6;
  --green: #2fbf71;
  --red: #ff5c5c;
  --yellow: #ffb84f;
  --radius: 14px;
  --sidebar-w: 240px;
  --mobile-topbar-h: 56px;
  --bottom-nav-h: 60px;
  font-size: 16px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  overscroll-behavior-y: contain;
}

/* Prevent iOS Safari from zooming in when a form field is focused —
   any input under 16px triggers it, so we floor the size on mobile. */
@media (max-width: 640px) {
  input, select, textarea { font-size: 16px !important; }
}

a { color: var(--primary); text-decoration: none; }

/* ---------- Auth screens ---------- */
.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(circle at 20% 20%, #1a2440 0%, var(--bg) 60%);
}
.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 36px 32px;
}
@media (max-width: 480px) {
  .auth-shell { padding: 16px; }
  .auth-card { padding: 26px 20px; }
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 24px;
}
.brand .logo { font-size: 28px; }
.brand .logo-img { height: 32px; width: auto; display: block; }
.brand h1 { font-size: 20px; margin: 0; }
.titleLogo { color: var(--yellow); }
.mobile-topbar .brand, #brandLogoSide { cursor: pointer; }
.user-chip { cursor: pointer; }
.auth-card h2 { font-size: 22px; margin: 0 0 6px; }
.auth-card p.sub { color: var(--text-dim); margin: 0 0 24px; font-size: 14px; }

label { display: block; font-size: 13px; color: var(--text-dim); margin: 14px 0 6px; }
input, select, textarea {
  width: 100%;
  padding: 11px 13px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--text);
  font-size: 14px;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); }

/* The browser's own dropdown arrow on <select> is drawn by the OS/browser,
   not styleable by color alone — on Chrome/Edge in particular it can come
   out dark and hard to see against this dark theme, the same problem as
   the date-picker icon above. Replacing it with an inline light-gray
   chevron (matching --text-dim) makes it visible everywhere a <select> is
   used, consistently across browsers, instead of relying on each browser's
   own (sometimes dark) default. */
select {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-color: var(--bg-soft);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='none' stroke='%239aa4bd' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='5 8 10 13 15 8'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 16px;
  padding-right: 36px;
}

button, .btn {
  cursor: pointer;
  border: none;
  border-radius: 10px;
  padding: 11px 18px;
  font-size: 14px;
  font-weight: 600;
  background: var(--primary);
  color: #fff;
  transition: background .15s ease;
}
button:hover, .btn:hover { background: var(--primary-dim); }
button.secondary, .btn.secondary { background: var(--bg-soft); border: 1px solid var(--border); color: var(--text); }
button.secondary:hover { background: var(--card-hover); }
button.danger { background: var(--red); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.full, .btn.full { width: 100%; margin-top: 20px; }

.password-field { position: relative; }
.password-field input { padding-right: 42px; }
.pw-toggle {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: none;
  padding: 6px 8px;
  font-size: 15px;
  line-height: 1;
  color: var(--text-dim);
  cursor: pointer;
}
.pw-toggle:hover { background: transparent; color: var(--text); }

.error-msg { color: var(--red); font-size: 13px; margin-top: 10px; min-height: 18px; }
.success-msg { color: var(--green); font-size: 13px; margin-top: 10px; min-height: 18px; }
.link-btn { background: none; border: none; padding: 0; color: var(--primary); font-size: 13px; cursor: pointer; }
.link-btn:hover { background: none; text-decoration: underline; }
.switch-line { text-align: center; margin-top: 20px; font-size: 14px; color: var(--text-dim); }

/* ---------- App shell ---------- */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
  min-height: 100dvh;
}
.sidebar {
  background: var(--bg-soft);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
}
.sidebar .brand { padding: 0 8px; margin-bottom: 28px; }
.nav-group { margin-bottom: 18px; }
.nav-label { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-dim); padding: 0 12px; margin-bottom: 6px; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
}
.nav-item:hover { background: var(--card-hover); color: var(--text); }
.nav-item.active { background: var(--primary); color: #fff; }
.sidebar-footer { margin-top: auto; padding-top: 14px; border-top: 1px solid var(--border); }
.user-chip { display: flex; align-items: center; gap: 10px; padding: 8px 12px; font-size: 13px; color: var(--text-dim); }
.user-chip .avatar {
  width: 30px; height: 30px; border-radius: 50%; background: var(--primary);
  display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; font-size: 13px;
}

/* Mobile-only topbar with hamburger button + backdrop for the sidebar drawer.
   Hidden entirely on desktop widths. */
.mobile-topbar { display: none; }
.sidebar-backdrop { display: none; }
.hamburger-btn {
  background: transparent; border: none; padding: 8px; color: var(--text);
  font-size: 22px; line-height: 1; cursor: pointer; border-radius: 8px;
}
.hamburger-btn:hover { background: var(--card-hover); }
.sidebar-close-btn { display: none; }

.main { padding: 28px 32px; overflow-y: auto; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 24px; flex-wrap: wrap; }
.topbar h2 { margin: 0; font-size: 24px; }
.topbar .sub { color: var(--text-dim); font-size: 14px; margin-top: 4px; }
#topbarActions { display: flex; gap: 8px; flex-wrap: wrap; }

.app-clock {
  font-size: 13px;
  color: var(--text-dim);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 12px;
  white-space: nowrap;
  line-height: 1.3;
}
.app-clock .clock-time { color: var(--text, #fff); font-weight: 600; margin-left: 6px; }
.mobile-topbar .app-clock { margin-left: auto; padding: 4px 10px; font-size: 12px; }

.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 1000px) {
  .grid.cols-4, .grid.cols-3, .grid.cols-2 { grid-template-columns: 1fr 1fr; }
}

/* ---------- Mobile layout (phones / small tablets) ---------- */
@media (max-width: 900px) {
  .app { display: block; }

  .mobile-topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    position: sticky;
    top: 0;
    z-index: 30;
    /* The safe-area inset (e.g. the Dynamic Island on newer iPhones) has to
       ADD to the bar's height, not eat into it — otherwise (with the global
       box-sizing: border-box) that inset shrinks the content box until the
       logo/hamburger no longer fit inside it and visibly overflow. */
    height: calc(var(--mobile-topbar-h) + env(safe-area-inset-top));
    padding: 0 12px;
    padding-top: env(safe-area-inset-top);
    background: var(--bg-soft);
    border-bottom: 1px solid var(--border);
  }
  .mobile-topbar .brand { margin: 0; gap: 8px; }
  .mobile-topbar .brand .logo { font-size: 20px; }
  .mobile-topbar .brand .logo-img { height: 24px; }
  .mobile-topbar .brand h1 { font-size: 15px; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(80vw, 300px);
    z-index: 60;
    transform: translateX(-100%);
    transition: transform .22s ease;
    padding-top: calc(16px + env(safe-area-inset-top));
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    box-shadow: 8px 0 24px rgba(0,0,0,.35);
    overflow-y: auto;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-close-btn {
    display: block;
    margin-left: auto;
    background: transparent; border: none; color: var(--text-dim);
    font-size: 22px; padding: 4px 8px; cursor: pointer;
  }
  .sidebar .brand { display: flex; align-items: center; }

  .sidebar-backdrop.open {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(6,9,16,.55);
    z-index: 55;
  }

  .main {
    padding: 16px;
    padding-bottom: calc(var(--bottom-nav-h) + 20px + env(safe-area-inset-bottom));
  }
  .topbar h2 { font-size: 20px; }

  /* The mobile topbar already shows its own clock (#appClockMobile) —
     without this, the desktop topbar's clock (#appClockDesktop) also
     renders below it, showing the time twice on phones. */
  #appClockDesktop { display: none; }

  .grid.cols-4, .grid.cols-3, .grid.cols-2 { grid-template-columns: 1fr; }

  .score-ring-wrap { flex-direction: column; align-items: stretch; text-align: center; }
}

@media (min-width: 901px) {
  .sidebar-close-btn, .mobile-topbar, .sidebar-backdrop { display: none !important; }
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.card h3 { margin: 0 0 4px; font-size: 15px; }
.card .metric { font-size: 28px; font-weight: 700; margin: 8px 0 2px; }
.card .metric-label { font-size: 13px; color: var(--text-dim); }
.card .metric.green { color: var(--green); }
.card .metric.red { color: var(--red); }

.section-title { font-size: 17px; font-weight: 600; margin: 28px 0 12px; }
.section-title:first-child { margin-top: 0; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; color: var(--text-dim); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; padding: 8px 10px; border-bottom: 1px solid var(--border); }
td { padding: 10px; border-bottom: 1px solid var(--border); }
tr:last-child td { border-bottom: none; }
tr.bill-row:hover td, tr.txn-row:hover td { background: var(--card-hover); }

/* Bills and Transactions: a full table on desktop, a condensed tap-to-edit
   card list on phones — a wide table either truncates columns or forces
   sideways scrolling on a small screen, neither of which reads well. */
.bills-cards, .txn-cards { display: none; }
@media (max-width: 768px) {
  .bills-table-wrap, .txn-table-wrap { display: none; }
  .bills-cards, .txn-cards { display: flex; flex-direction: column; gap: 10px; }
}
.bill-card, .txn-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  cursor: pointer;
}
.bill-card:hover, .bill-card:active, .txn-card:hover, .txn-card:active { background: var(--card-hover); }
.bill-card-top, .txn-card-top { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.bill-card-name, .txn-card-merchant { font-weight: 600; font-size: 15px; }
.bill-card-amount { font-weight: 700; font-size: 16px; white-space: nowrap; }
.txn-card-top .amount-neg, .txn-card-top .amount-pos { font-size: 16px; white-space: nowrap; }
.amount-neg { color: var(--red); font-weight: 600; }
.amount-pos { color: var(--green); font-weight: 600; }

/* Wide tables scroll horizontally inside their card instead of squashing
   or breaking the page layout on narrow screens. */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: -4px; padding: 4px; }
.table-scroll table { min-width: 560px; }
@media (max-width: 640px) {
  .table-scroll table { min-width: 480px; }
}

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 100px; font-size: 12px; font-weight: 600; }
.badge.info { background: rgba(79,124,255,.15); color: #7fa0ff; }
.badge.warning { background: rgba(255,184,79,.15); color: var(--yellow); }
.badge.critical { background: rgba(255,92,92,.15); color: var(--red); }

.progress-bar { height: 8px; background: var(--bg-soft); border-radius: 100px; overflow: hidden; margin: 8px 0; }
.progress-bar > div { height: 100%; background: var(--primary); border-radius: 100px; }
.progress-bar.over > div { background: var(--red); }

.toolbar { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; align-items: center; }
.toolbar input, .toolbar select { width: auto; }
.spacer { flex: 1; }
@media (max-width: 640px) {
  .toolbar input, .toolbar select { width: 100%; }
}

.empty-state { text-align: center; padding: 40px 20px; color: var(--text-dim); }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(6,9,16,.6);
  display: flex; align-items: center; justify-content: center; z-index: 70;
  padding: 16px;
}
.modal { position: relative; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; width: 100%; max-width: 440px; max-height: 90vh; overflow-y: auto; }
.modal h3 { margin: 0 32px 0 0; }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }
.modal-actions button { flex: 1; }
.hidden { display: none !important; }

.modal-close-btn {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 15px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  z-index: 2;
}
.modal-close-btn:hover { background: var(--card-hover); color: var(--text); }

/* On phones, a modal takes over the whole screen edge-to-edge instead of
   floating as a card with a background gap around it — a bottom sheet that
   doesn't reach full height reads as a rendering glitch on a small screen,
   and a full-screen sheet is also just easier to fill in with a keyboard
   up. The close button stays pinned to the viewport (not the scrolling
   modal) so it's reachable no matter how far the form scrolls. */
@media (max-width: 640px) {
  .modal-backdrop { align-items: stretch; justify-content: stretch; padding: 0; }
  .modal {
    max-width: 100%;
    width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
    padding: calc(20px + env(safe-area-inset-top)) 18px calc(20px + env(safe-area-inset-bottom));
  }
  .modal-close-btn {
    position: fixed;
    top: calc(14px + env(safe-area-inset-top));
    right: 14px;
    background: var(--card-hover);
  }
}

.score-ring-wrap { display: flex; align-items: center; gap: 24px; }
.score-factors { flex: 1; }
.score-factor-row { display: flex; justify-content: space-between; font-size: 13px; margin: 8px 0; color: var(--text-dim); }
.score-factor-row span:last-child { color: var(--text); font-weight: 600; }

.cat-icon { display: inline-flex; width: 28px; height: 28px; border-radius: 8px; align-items: center; justify-content: center; font-size: 14px; margin-right: 8px; }

.cat-manage-list { display: flex; flex-direction: column; gap: 10px; }
.cat-manage-row { display: flex; align-items: center; gap: 4px; padding: 8px 0; border-bottom: 1px solid var(--border, #232a3a); flex-wrap: wrap; }
.cat-manage-row:last-child { border-bottom: none; }
.cat-manage-name { flex: 1 1 160px; font-size: 14px; min-width: 160px; }

/* Account rows can carry up to three buttons (credit cards get Update
   Balance + Pay Balance + Remove). Grouping them lets the whole cluster
   drop to its own line on narrow screens instead of the row overflowing
   and buttons overlapping each other. */
.acct-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-left: auto; }
@media (max-width: 480px) {
  .cat-manage-row { gap: 10px; }
  .acct-actions { width: 100%; margin-left: 0; justify-content: flex-end; }
  .acct-actions button { flex: 1 1 auto; padding: 10px 10px; font-size: 13px; }
}
input[type="color"] { padding: 4px; height: 40px; cursor: pointer; }

/* Chrome/Edge/Safari draw the calendar/clock picker icon on <input
   type="date|month|time|week|datetime-local"> in a dark color meant for
   light backgrounds — on this dark theme it's nearly invisible (e.g. the
   Card Activity month picker). Inverting it flips dark-on-transparent to
   light-on-transparent so it's visible everywhere one of these inputs is
   used, without needing to touch each input individually. Firefox doesn't
   support ::-webkit-calendar-picker-indicator and just shows its own
   (already light-colored) icon, so this is a no-op there rather than a
   problem. */
input[type="date"]::-webkit-calendar-picker-indicator,
input[type="month"]::-webkit-calendar-picker-indicator,
input[type="week"]::-webkit-calendar-picker-indicator,
input[type="time"]::-webkit-calendar-picker-indicator,
input[type="datetime-local"]::-webkit-calendar-picker-indicator {
  filter: invert(1);
  cursor: pointer;
}

/* Mini calendar day-picker — used to set semimonthly pay days (e.g. the
   7th & 22nd) by clicking rather than typing an anchor date. */
.mini-calendar {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
  margin: 4px 0 8px;
}
.mini-cal-day {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 0;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}
.mini-cal-day:hover { background: var(--card-hover); }
.mini-cal-day.selected { background: var(--primary); border-color: var(--primary); color: #fff; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }

/* ---------- Bottom tab bar (mobile only) ---------- */
.bottom-nav { display: none; }
@media (max-width: 900px) {
  .bottom-nav {
    display: flex;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    /* Same fix as .mobile-topbar: grow to accommodate the home-indicator
       safe area instead of letting it shrink the content box. */
    height: calc(var(--bottom-nav-h) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--bg-soft);
    border-top: 1px solid var(--border);
    z-index: 40;
  }
  .bottom-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    color: var(--text-dim);
    font-size: 10px;
    font-weight: 600;
    cursor: pointer;
    background: none;
    border: none;
  }
  .bottom-nav-item .icon { font-size: 19px; }
  .bottom-nav-item.active { color: var(--primary); }
}