:root {
  color-scheme: dark;
  --bg: #08060f;
  --surface: #0e0b18;
  --surface-2: #171226;
  --border: rgba(255, 255, 255, 0.1);
  --border-soft: rgba(255, 255, 255, 0.07);
  --text: #f2eeff;
  --text-dim: rgba(242, 238, 255, 0.6);
  --text-faint: rgba(242, 238, 255, 0.4);
  --accent: #8b5cf6;
  --accent-strong: #7c3aed;
  --accent-soft: rgba(139, 92, 246, 0.16);
  --acid: #c7ff3c;
  --acid-soft: rgba(199, 255, 60, 0.14);
  --plan: #ff8a3d;
  --alert: #ff3b5c;
  --alert-soft: rgba(255, 59, 92, 0.14);
  --font-body: "Space Grotesk", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
}

* {
  box-sizing: border-box;
}

/* Safety net: nothing on the page (an open popup menu especially) should
   ever be able to widen the page and let it scroll sideways on mobile. */
html, body {
  overflow-x: hidden;
}

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  max-width: 720px;
  margin: 2rem auto;
  padding: 0 1rem 6rem;
  line-height: 1.5;
}

@media (min-width: 900px) {
  body {
    max-width: 1100px;
    padding-bottom: 0;
  }
}

h1 {
  margin-bottom: 0.25rem;
}

.auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 80vh;
}

.auth-screen.hidden {
  display: none;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  width: min(90vw, 320px);
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
}

.auth-form h1 {
  text-align: center;
  margin-bottom: 0.5rem;
}

.auth-switch {
  margin: 0.25rem 0 0;
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.link-btn {
  background: transparent;
  border: none;
  padding: 0;
  color: var(--acid);
  font: inherit;
  cursor: pointer;
  text-decoration: underline;
}

.link-btn:hover {
  opacity: 0.85;
}

.app-header-user {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.9rem;
  color: var(--text-dim);
}

.tabs {
  display: flex;
  gap: 1.25rem;
  margin: 1.25rem 0 0;
  border-bottom: 1px solid var(--border-soft);
}

.tab-btn {
  background: transparent;
  color: var(--text-dim);
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  padding: 0.6rem 0.2rem;
  margin-bottom: -1px;
}

.tab-btn:hover {
  color: var(--text);
}

.tab-btn.active {
  color: var(--acid);
  border-bottom-color: var(--acid);
  font-weight: 600;
}

/* Mobile/default: the sidebar lays out as a header bar — brand + user info
   on one row, tabs as a horizontal pill row below. Desktop turns this into
   an actual left-hand sidebar (see the min-width: 900px block below). */
.sidebar {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "brand user" "nav nav";
  align-items: center;
  column-gap: 1rem;
}

.sidebar-brand {
  grid-area: brand;
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: 0.04em;
  color: var(--acid);
}

.app-version {
  font-family: var(--font-mono);
  font-weight: 400;
  font-size: 0.65rem;
  letter-spacing: 0;
  color: var(--text-faint);
  vertical-align: super;
}

.sidebar .app-header-user {
  grid-area: user;
}

.sidebar .tabs {
  grid-area: nav;
}

@media (min-width: 900px) {
  main {
    display: flex;
    align-items: flex-start;
    gap: 2.5rem;
  }

  .sidebar {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 2rem;
    flex: 0 0 220px;
    position: sticky;
    top: 2rem;
    padding: 1.5rem 1.25rem;
    background: var(--surface);
    border: 1px solid var(--border-soft);
    border-radius: 20px;
  }

  .sidebar-brand {
    font-size: 1.25rem;
  }

  .sidebar .tabs {
    flex-direction: column;
    gap: 0.25rem;
    margin: 0;
    border-bottom: none;
  }

  .sidebar .tab-btn {
    text-align: left;
    border-bottom: none;
    border-radius: 10px;
    padding: 0.65rem 0.8rem;
    margin-bottom: 0;
  }

  .sidebar .tab-btn.active {
    background: var(--acid-soft);
    color: var(--acid);
  }

  .sidebar .app-header-user {
    margin-top: auto;
    flex-wrap: wrap;
  }

  .main-content {
    flex: 1;
    min-width: 0;
  }
}

section {
  margin: 2rem 0;
  padding: 1.25rem;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 20px;
}

.table-scroll {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1rem;
}

th,
td {
  text-align: left;
  padding: 0.6rem 0.5rem;
  border-bottom: 1px solid var(--border-soft);
}

th {
  color: var(--text-faint);
  font-size: 0.8rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

tbody tr:hover td {
  background: rgba(255, 255, 255, 0.03);
}

.amount {
  text-align: right;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.amount.negative {
  color: var(--alert);
}

.field-balance {
  font-size: 0.8rem;
  color: var(--text-faint);
}

.field-balance:empty {
  display: none;
}

#history-table .amount {
  color: var(--text);
}

form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: end;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.9rem;
  color: var(--text-dim);
}

input,
select,
button {
  font: inherit;
  padding: 0.5rem 0.7rem;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
}

input:focus,
select:focus {
  outline: none;
  border-color: var(--acid);
  box-shadow: 0 0 0 3px rgba(199, 255, 60, 0.18);
}

button {
  background: var(--acid);
  color: var(--surface);
  border-color: var(--acid);
  font-weight: 600;
  cursor: pointer;
}

button:hover {
  opacity: 0.9;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.hidden {
  display: none;
}

.goal-note {
  margin-top: 0.2rem;
  font-size: 0.8rem;
  color: var(--text-faint);
}

.goal-bar {
  margin-top: 0.35rem;
  width: 100%;
  max-width: 180px;
  height: 6px;
  border-radius: 4px;
  background: var(--plan);
  overflow: hidden;
}

.goal-bar-fill {
  height: 100%;
  border-radius: 4px;
  background: var(--acid);
}

.goal-bar-fill.negative {
  background: var(--alert);
}

.checkbox-label {
  flex-direction: row !important;
  align-items: center;
  gap: 0.4rem !important;
}

.checkbox-label input[type="checkbox"] {
  width: auto;
  padding: 0;
}

.repeat-badge {
  color: var(--text-faint);
  font-size: 0.9em;
}

.confirmed-badge {
  color: var(--acid);
  font-weight: 700;
}

.unconfirmed-badge {
  color: var(--text-faint);
  font-weight: 700;
}

.date-group-row td {
  padding-top: 0.9rem;
  padding-bottom: 0.35rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  background: var(--surface-2);
  border-bottom: none;
}

.date-group-row:first-child td {
  padding-top: 0.5rem;
}

.category-group-row td {
  padding-top: 0.9rem;
  padding-bottom: 0.35rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  background: var(--surface-2);
  border-bottom: none;
  cursor: pointer;
}

.category-group-row:first-child td {
  padding-top: 0.5rem;
}

.rta-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
  padding: 1.1rem 1.4rem;
  border: 1px solid rgba(199, 255, 60, 0.45);
  border-radius: 18px;
  background: linear-gradient(120deg, var(--acid-soft), var(--accent-soft));
}

.rta-card.hidden {
  display: none;
}

.rta-card-info {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  flex-wrap: wrap;
}

.rta-card .rta-label {
  font-weight: 600;
}

.rta-card .amount {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--acid);
}

.allocate-remaining {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
  padding: 0.75rem 1.1rem;
  border-radius: 14px;
  background: var(--acid-soft);
  border: 1px solid rgba(199, 255, 60, 0.45);
}

.allocate-remaining.negative {
  background: var(--alert-soft);
  border-color: rgba(255, 59, 92, 0.45);
}

.allocate-remaining-label {
  font-weight: 600;
  color: var(--text-dim);
}

.allocate-remaining .amount {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--acid);
}

.allocate-remaining.negative .amount {
  color: var(--alert);
}

.allocate-envelope-list {
  max-height: 50vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin-bottom: 0.75rem;
  padding-right: 0.25rem;
}

.allocate-group-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-top: 0.7rem;
}

.allocate-group-label:first-child {
  margin-top: 0;
}

.allocate-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--border-soft);
}

.allocate-row-info {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}

.allocate-row-name {
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.allocate-row-balance {
  font-size: 0.8rem;
  color: var(--text-faint);
}

.allocate-row-input {
  width: 110px;
  flex: none;
  text-align: right;
}

.filter-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.filter-summary {
  font-size: 0.85rem;
  color: var(--text-dim);
}

.history-search {
  min-width: 220px;
}

.form-hint {
  margin: -0.25rem 0 0.25rem;
  font-size: 0.8rem;
  color: var(--text-faint);
}

.menu-anchor {
  position: relative;
  display: inline-block;
}

.menu-btn {
  background: transparent;
  color: var(--text-dim);
  border-color: var(--border);
}

.menu {
  position: absolute;
  top: calc(100% + 0.25rem);
  left: 0;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  min-width: 180px;
  max-width: min(220px, calc(100vw - 2rem));
  padding: 0.75rem;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
}

/* Used by menus whose anchor button sits near the right edge of the screen
   (the envelope-row and operation-row "⋮" menus): opens the popup leftward
   from the button instead of rightward, so the "⋮" ends up at the menu's
   end rather than its start and the popup never overflows off-screen. */
.menu-align-end {
  left: auto;
  right: 0;
}

.menu-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  color: var(--text);
  border: none;
  padding: 0.4rem 0.2rem;
  cursor: pointer;
}

.menu-item:hover {
  color: var(--acid);
  opacity: 1;
}

.menu.hidden {
  display: none;
}

.message {
  min-height: 1.5rem;
  font-size: 0.9rem;
}

.message.error {
  color: var(--alert);
}

.message.success {
  color: var(--acid);
}

.btn-secondary {
  background: transparent;
  color: var(--text-dim);
  border-color: var(--border);
}

/* .btn-secondary is applied to a <button> everywhere else, which already
   gets its font/padding/border from the shared "input, select, button" base
   rule — an <a> needs that base laid out explicitly, plus the link-specific
   resets (no underline, block layout so padding applies predictably). */
a.btn-secondary {
  display: inline-block;
  font: inherit;
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  text-decoration: none;
  cursor: pointer;
}

a.btn-secondary:hover {
  opacity: 0.9;
}

.stats-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.stats-header h2 {
  margin: 0;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(0, 0, 0, 0.65);
  z-index: 100;
}

.modal-overlay.hidden {
  display: none;
}

.modal {
  width: min(90vw, 420px);
  max-height: 90vh;
  overflow-y: auto;
  padding: 1.5rem;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 22px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.6);
}

.modal-wide {
  width: min(94vw, 560px);
}

.modal h3 {
  margin-top: 0;
}

.modal form {
  flex-direction: column;
  align-items: stretch;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.empty-state {
  color: var(--text-faint);
  font-size: 0.9rem;
  padding: 0.5rem 0;
}

.stats-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.stats-summary-card {
  flex: 1 1 160px;
  padding: 1rem 1.2rem;
  background: var(--surface-2);
  border: 1px solid var(--border-soft);
  border-radius: 16px;
}

.stats-summary-label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 0.4rem;
}

.stats-summary-amount {
  display: block;
  text-align: left;
  font-size: 1.5rem;
  font-weight: 700;
}

.stats-summary-amount.income {
  color: var(--acid);
}

.stats-summary-amount.expense {
  color: var(--alert);
}

.stats-summary-amount.total {
  color: var(--accent);
}

.stats-summary-amount.total.negative {
  color: var(--alert);
}

.pie-chart-wrap {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1.5rem;
}

.pie-chart {
  flex: 0 0 auto;
  width: 180px;
  height: 180px;
  border-radius: 50%;
}

.pie-legend {
  flex: 1 1 220px;
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.pie-legend li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
}

.pie-legend-swatch {
  flex: 0 0 auto;
  width: 12px;
  height: 12px;
  border-radius: 3px;
}

.pie-legend-name {
  flex: 1 1 auto;
}

.pie-legend-amount {
  flex: 0 0 auto;
  color: var(--text-dim);
  font-size: 0.85rem;
}

#stats-category-select {
  margin-bottom: 1rem;
}

.dynamics-chart {
  display: flex;
  align-items: stretch;
  gap: 1.5rem;
  min-width: min-content;
  padding-top: 1.4rem;
}

.dynamics-month {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 0 0 auto;
  width: 64px;
}

.dynamics-half {
  width: 100%;
  height: 90px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.dynamics-half-top {
  justify-content: flex-end;
}

.dynamics-half-bottom {
  justify-content: flex-start;
}

.dynamics-bar {
  width: 22px;
}

.dynamics-half-top .dynamics-bar {
  border-radius: 4px 4px 0 0;
}

.dynamics-half-bottom .dynamics-bar {
  border-radius: 0 0 4px 4px;
}

.dynamics-bar.inflow {
  background: var(--acid);
}

.dynamics-bar.outflow {
  background: var(--alert);
}

.dynamics-baseline {
  width: 100%;
  height: 1px;
  background: var(--border);
}

.dynamics-value {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-faint);
  margin: 0.2rem 0;
  white-space: nowrap;
}

.dynamics-month-label {
  margin-top: 0.5rem;
  font-size: 0.75rem;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stats-highlight-card {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.stats-highlight-name {
  font-size: 1.05rem;
  font-weight: 600;
}

.stats-highlight-amount {
  font-family: var(--font-mono);
  color: var(--acid);
}

.stats-highlight-amount.expense {
  color: var(--alert);
}

.stats-highlight-amount.negative {
  color: var(--alert);
}

.fab-anchor {
  position: fixed;
  right: 1.5rem;
  bottom: 1.5rem;
  z-index: 50;
  pointer-events: none;
}

.fab-anchor .fab-btn,
.fab-anchor .fab-menu {
  pointer-events: auto;
}

.fab-btn {
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background: linear-gradient(150deg, var(--accent), var(--accent-strong));
  border: 1px solid rgba(199, 255, 60, 0.5);
  color: var(--text);
  font-size: 1.8rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 30px rgba(124, 58, 237, 0.55);
  cursor: pointer;
}

.fab-btn:hover {
  opacity: 1;
  filter: brightness(1.08);
}

.fab-menu {
  top: auto;
  bottom: calc(100% + 0.6rem);
  left: auto;
  right: 0;
  min-width: 170px;
}

.settings-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  max-width: 420px;
}
