:root {
  /* Neutral surfaces */
  --bg: #FAFBFD;
  --surface: #FFFFFF;
  --border: #EEF1F6;
  --border-strong: #DEE3EC;

  /* Text */
  --text: #1F2430;
  --text-muted: #6E7383;
  --text-faint: #A2A7B7;

  /* Primary accent (brand / balance / active states) — a clean, confident blue */
  --primary: #2563EB;
  --primary-hover: #1D4ED8;
  --primary-soft: #EEF4FE;
  --primary-soft-strong: #DCE8FD;
  --primary-shadow: rgba(37, 99, 235, 0.22);

  /* Semantic colors */
  --income: #16A34A;
  --income-hover: #128A3E;
  --income-soft: #E7F7ED;
  --income-shadow: rgba(22, 163, 74, 0.20);

  --expense: #E0392B;
  --expense-hover: #C62E22;
  --expense-soft: #FCEAE8;
  --expense-shadow: rgba(224, 57, 43, 0.20);

  --saving: #0D9488;
  --saving-hover: #0B7C72;
  --saving-soft: #E1F5F3;
  --saving-shadow: rgba(13, 148, 136, 0.20);

  --amber: #D97706;
  --amber-hover: #B96406;
  --amber-soft: #FDF1E0;
  --amber-shadow: rgba(217, 119, 6, 0.20);

  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 9px;
  --shadow-card: 0 1px 2px rgba(23, 27, 38, 0.02), 0 4px 12px rgba(23, 27, 38, 0.03);
  --shadow-btn: 0 1px 2px rgba(23, 27, 38, 0.05);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
}

button, input, select {
  font-family: inherit;
}

.app {
  display: flex;
  min-height: 100vh;
}

/* ---------------- Spine (sidebar) ---------------- */

.spine {
  width: 296px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  color: var(--text);
  display: flex;
  flex-direction: column;
  padding: 32px 26px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.spine-top {
  display: flex;
  flex-direction: column;
  margin-bottom: 32px;
}

.spine-top-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 9px;
  border-radius: 100px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-faint);
  font-size: 12px;
  font-weight: 500;
  text-decoration: none;
  flex-shrink: 0;
  transition: background 0.15s ease, color 0.15s ease;
}

.user-chip, .user-chip * { text-decoration: none; }

.user-chip:hover {
  background: var(--expense-soft);
  color: var(--expense);
}

.user-chip-avatar {
  width: 18px;
  height: 18px;
  max-width: 18px;
  max-height: 18px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  display: block;
}

.brand {
  font-family: "Sora", sans-serif;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.3px;
  color: var(--text);
}

.brand-sub {
  font-size: 13px;
  color: var(--text-faint);
  margin-top: 2px;
}

.balance-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  background: linear-gradient(160deg, var(--primary-soft) 0%, #F7FAFE 100%);
  border: 1px solid var(--primary-soft-strong);
  border-radius: var(--radius-lg);
  padding: 22px 16px;
  margin-bottom: 22px;
  box-shadow: 0 3px 10px rgba(37, 99, 235, 0.06);
}

.balance-label-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin-bottom: 8px;
}

.balance-label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--primary);
  margin-bottom: 0;
}

.balance-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  padding: 0;
  border: none;
  background: none;
  color: var(--primary);
  opacity: 0.6;
  cursor: pointer;
  border-radius: 6px;
  transition: opacity 0.15s ease, background 0.15s ease;
}

.balance-toggle:hover {
  opacity: 1;
  background: var(--primary-soft-strong);
}

.balance-toggle svg { width: 16px; height: 16px; }

.balance-figure {
  font-family: "Sora", sans-serif;
  font-size: 38px;
  font-weight: 700;
  color: var(--primary);
  line-height: 1.15;
  display: block;
  white-space: nowrap;
  max-width: 100%;
  word-break: break-word;
  letter-spacing: -0.5px;
}

.balance-note {
  font-size: 12px;
  color: var(--text-faint);
  margin-top: 10px;
  line-height: 1.4;
}

.spine-stats {
  margin: 0 0 24px 0;
}

.stat-row {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 3px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14.5px;
}

.stat-row:last-child { border-bottom: none; }

.stat-row dt { color: var(--text-faint); font-weight: 500; margin: 0; font-size: 12px; }
.stat-row dd { margin: 0; font-weight: 700; font-size: 15.5px; color: var(--text); }
.stat-row dd.in { color: var(--income); }
.stat-row dd.out { color: var(--expense); }
.stat-row dd.save { color: var(--saving); }

.spine-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: auto;
}

.nav-btn {
  position: relative;
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 14.5px;
  font-weight: 500;
  padding: 10px 14px 10px 18px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.nav-btn::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 0;
  border-radius: 3px;
  background: var(--primary);
  transition: height 0.15s ease;
}

.nav-btn:hover {
  background: var(--bg);
  color: var(--text);
}

.nav-btn.is-active {
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 600;
}

.nav-btn.is-active::before {
  height: 18px;
}

/* ---------------- Content ---------------- */

.content {
  flex: 1;
  min-width: 0;
  padding: 44px 52px 80px;
  max-width: 1220px;
  margin: 0 auto;
}

.view { display: none; }
.view.is-active { display: block; }

.view-header {
  margin-bottom: 30px;
  text-align: center;
}

.view-header h1 {
  font-family: "Sora", sans-serif;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.4px;
  margin: 0 0 6px 0;
  color: var(--text);
}

.view-header p {
  color: var(--text-muted);
  margin: 0;
  font-size: 15px;
}

/* Charts */

.chart-pair {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  margin-bottom: 36px;
}

.chart-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px 22px 16px;
  min-width: 0;
  box-shadow: var(--shadow-card);
}

.chart-canvas-wrap {
  max-width: 260px;
  margin: 0 auto;
}

.chart-block h2 {
  font-family: "Sora", sans-serif;
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 12px 0;
  text-align: center;
  color: var(--text);
}

/* Month strip (Overview) */

.month-strip {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 26px;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 24px;
  margin-bottom: 26px;
  box-shadow: var(--shadow-card);
  font-size: 14px;
}

.month-strip-label {
  font-family: "Sora", sans-serif;
  font-weight: 700;
  color: var(--text);
}

.month-strip-item { color: var(--text-muted); }
.month-strip-item strong { color: var(--text); margin-left: 6px; font-weight: 700; }
.month-strip-item strong.in { color: var(--income); }
.month-strip-item strong.out { color: var(--expense); }
.month-strip-item strong.save { color: var(--saving); }

/* History */

.history-month {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 14px;
  overflow: hidden;
  box-shadow: var(--shadow-card);
}

.history-summary {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 22px;
  flex-wrap: wrap;
  background: none;
  border: none;
  cursor: pointer;
  padding: 16px 20px;
  font-size: 14.5px;
  text-align: center;
  border-radius: var(--radius-lg);
  transition: background 0.15s ease;
}

.history-summary:hover { background: var(--bg); }

.history-month-name {
  font-family: "Sora", sans-serif;
  font-size: 15.5px;
  font-weight: 700;
  min-width: 130px;
  color: var(--text);
}

.history-summary .in { color: var(--income); font-weight: 600; }
.history-summary .out { color: var(--expense); font-weight: 600; }
.history-summary .save { color: var(--saving); font-weight: 600; }
.history-summary .caret { color: var(--text-faint); margin-left: 4px; transition: transform 0.15s ease; }

.history-details { display: none; border-top: 1px solid var(--border); padding: 6px 20px 16px; }
.history-month.is-open .history-details { display: block; }
.history-month.is-open .caret { transform: rotate(180deg); display: inline-block; }

.empty-note {
  color: var(--text-faint);
  font-size: 13.5px;
  text-align: center;
  margin-top: 10px;
}

/* Quick add forms */

.quick-add-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 22px;
}

@media (max-width: 1000px) {
  .quick-add-row { grid-template-columns: 1fr 1fr; }
  .chart-pair { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 700px) {
  .quick-add-row { grid-template-columns: 1fr; }
  .chart-pair { grid-template-columns: 1fr; }
}

.quick-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px 24px;
  min-width: 0;
  box-shadow: var(--shadow-card);
}

.quick-form h2 {
  font-family: "Sora", sans-serif;
  font-size: 16px;
  font-weight: 700;
  margin: 0 0 16px 0;
  text-align: center;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.quick-form h2::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.in-form h2::before { background: var(--income); }
.save-form h2::before { background: var(--saving); }
.out-form h2::before { background: var(--expense); }

.form-hint {
  font-size: 12px;
  color: var(--text-faint);
  margin: 8px 0 0;
  text-align: center;
}

.field-row {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.quick-add-row .field-row {
  flex-direction: column;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
  min-width: 0;
}

.field-narrow { flex: 0 0 110px; }
.field-narrow-md { flex: 0 0 160px; }

.filter-bar {
  display: flex;
  gap: 12px;
  margin-bottom: 18px;
}

.filter-search { flex: 1; }

.field-label {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-faint);
  margin: 0 0 0 2px;
}

.field-row input,
.field-row select {
  flex: 1;
  width: 100%;
  min-width: 0;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 11px 13px;
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field-row select { cursor: pointer; }

.field-row input::placeholder { color: var(--text-faint); }

.field-row input:focus,
.field-row select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

/* Buttons */

.quick-form button,
.fixed-form button,
.savings-account-form button,
.category-form button {
  width: 100%;
  margin-top: 6px;
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
  color: #fff;
  background: var(--primary);
  box-shadow: var(--shadow-btn);
  transition: background 0.15s ease, transform 0.08s ease, box-shadow 0.15s ease;
}

.quick-form button:hover,
.fixed-form button:hover,
.savings-account-form button:hover,
.category-form button:hover,
.credit-card-form button:hover,
.purchase-form button:hover {
  box-shadow: 0 6px 16px var(--primary-shadow);
  transform: translateY(-1px);
}

.quick-form button:active,
.fixed-form button:active,
.savings-account-form button:active,
.category-form button:active,
.credit-card-form button:active,
.purchase-form button:active {
  transform: translateY(0);
  box-shadow: var(--shadow-btn);
}

.in-form button { background: var(--income); }
.in-form button:hover { background: var(--income-hover); box-shadow: 0 6px 16px var(--income-shadow); }

.out-form button { background: var(--expense); }
.out-form button:hover { background: var(--expense-hover); box-shadow: 0 6px 16px var(--expense-shadow); }

.save-form button { background: var(--saving); }
.save-form button:hover { background: var(--saving-hover); box-shadow: 0 6px 16px var(--saving-shadow); }

.savings-account-form button { background: var(--saving); }
.savings-account-form button:hover { background: var(--saving-hover); box-shadow: 0 6px 16px var(--saving-shadow); }

.category-form button { background: var(--saving); }
.category-form button:hover { background: var(--saving-hover); box-shadow: 0 6px 16px var(--saving-shadow); }

.credit-card-form button { background: var(--amber); }
.credit-card-form button:hover { background: var(--amber-hover); box-shadow: 0 6px 16px var(--amber-shadow); }

.purchase-form button { background: var(--expense); }
.purchase-form button:hover { background: var(--expense-hover); box-shadow: 0 6px 16px var(--expense-shadow); }

.fixed-form { margin-bottom: 30px; }

.savings-account-form { margin-bottom: 30px; }

.category-form { margin-bottom: 30px; }

.credit-card-form { margin-bottom: 30px; }

.purchase-form { margin-top: 26px; }
.purchase-form h2 {
  font-family: "Sora", sans-serif;
  font-size: 16px;
  font-weight: 700;
  margin: 0 0 16px 0;
  text-align: center;
  color: var(--text);
}

/* Tables */

.ledger-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14.5px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-card);
}

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

.ledger-table thead th {
  text-align: left;
  font-weight: 600;
  color: var(--text-muted);
  font-size: 12.5px;
  padding: 12px 14px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.ledger-table th.num, .ledger-table td.num { text-align: right; }

.ledger-table tbody td {
  padding: 13px 14px;
  border-bottom: 1px solid var(--border);
}

.ledger-table tbody tr:last-child td { border-bottom: none; }

.ledger-table tbody tr:hover { background: var(--bg); }

.tag {
  display: inline-block;
  font-size: 12px;
  font-weight: 500;
  padding: 3px 10px;
  border-radius: 100px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.type-tag {
  display: inline-block;
  font-size: 12px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 100px;
}

.type-tag.income { background: var(--income-soft); color: var(--income); }
.type-tag.expense { background: var(--expense-soft); color: var(--expense); }
.type-tag.saving { background: var(--saving-soft); color: var(--saving); }

.amount-in { color: var(--income); font-weight: 700; }
.amount-out { color: var(--expense); font-weight: 700; }
.amount-save { color: var(--saving); font-weight: 700; }

.row-delete {
  background: none;
  border: 1px solid transparent;
  color: var(--text-faint);
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 500;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.row-delete:hover {
  background: var(--expense-soft);
  color: var(--expense);
  border-color: var(--expense-soft);
}

.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

.row-edit {
  background: none;
  border: 1px solid transparent;
  color: var(--text-faint);
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 500;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.row-edit:hover {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: var(--primary-soft-strong);
}

.edit-input {
  width: 100%;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 7px 9px;
  font-size: 14px;
  background: var(--surface);
  font-family: inherit;
  color: var(--text);
}

.edit-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.edit-input.due-day { width: 64px; }

.fixed-total-row {
  display: flex;
  justify-content: center;
  align-items: baseline;
  gap: 14px;
  margin-top: 18px;
  padding: 16px 20px;
  background: var(--primary-soft);
  border: 1px solid var(--primary-soft-strong);
  border-radius: var(--radius-lg);
  box-shadow: 0 3px 10px rgba(37, 99, 235, 0.05);
  font-size: 15px;
  text-align: center;
  color: var(--text-muted);
}

.fixed-total-row strong {
  font-family: "Sora", sans-serif;
  font-size: 20px;
  color: var(--primary);
}

/* ---------------- Responsive ---------------- */

@media (max-width: 900px) {
  .app { flex-direction: column; }

  .spine {
    width: 100%;
    height: auto;
    position: relative;
    padding: 20px 18px;
  }

  .content { padding: 28px 16px 56px; }

  .chart-pair, .quick-add-row { grid-template-columns: 1fr; }

  /* Stats in a compact 2-column grid instead of one tall stacked column */
  .spine-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2px 12px;
    margin-bottom: 18px;
  }
  .stat-row { border-bottom: none; padding: 8px 0; }
  .stat-row:nth-child(-n+2) { border-bottom: 1px solid var(--border); }

  /* Nav becomes a horizontally scrollable row of pills */
  .spine-nav {
    flex-direction: row;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 6px;
    margin-bottom: 0;
    padding-bottom: 4px;
  }
  .nav-btn {
    flex: 0 0 auto;
    white-space: nowrap;
    padding: 9px 14px;
  }
  .nav-btn::before {
    top: auto;
    bottom: 0;
    left: 12px;
    transform: none;
    width: 0;
    height: 3px;
  }
  .nav-btn.is-active::before { width: 60%; height: 3px; }

  /* Stack any remaining side-by-side fields (Fixed/Savings/Category forms) */
  .field-row { flex-direction: column; }
  .field-narrow { flex: 1 0 auto; }
  .field-narrow-md { flex: 1 0 auto; }

  /* Slightly larger tap targets for row actions on touch screens */
  .row-edit, .row-delete {
    padding: 7px 12px;
    font-size: 13px;
  }

  /* Tables become a stacked list of cards instead of scrolling sideways */
  .table-scroll { overflow-x: visible; }

  .ledger-table {
    border: none;
    box-shadow: none;
    background: transparent;
    border-radius: 0;
  }

  .ledger-table thead { display: none; }

  .ledger-table, .ledger-table tbody { display: block; width: 100%; }

  .ledger-table tr {
    display: block;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-card);
    margin-bottom: 10px;
    padding: 2px 0;
  }

  .ledger-table tr:last-child { margin-bottom: 0; }

  .ledger-table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 9px 14px;
    border-bottom: 1px solid var(--border);
    text-align: right;
  }

  .ledger-table td:last-child { border-bottom: none; }

  .ledger-table td[data-label]::before {
    content: attr(data-label);
    font-size: 12px;
    font-weight: 600;
    color: var(--text-faint);
    text-align: left;
    flex-shrink: 0;
  }

  .ledger-table .row-actions { width: 100%; justify-content: flex-end; }
  .ledger-table .edit-input { text-align: right; }
}

@media (max-width: 480px) {
  .balance-figure { font-size: 32px; }
  .view-header h1 { font-size: 24px; }

  .month-strip {
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    padding: 14px 18px;
  }
  .month-strip-label {
    font-size: 16px;
    text-align: center;
    padding-bottom: 10px;
    margin-bottom: 6px;
    border-bottom: 1px solid var(--border);
  }
  .month-strip-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 7px 0;
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
