:root {
  --bg: #FAF6EE;
  --surface: #FFFFFF;
  --ink: #20261F;
  --muted: #6B6255;
  --line: #E1D8C4;
  --primary: #1B4332;
  --primary-dark: #143528;
  --income: #3F6B4A;
  --expense: #9C4A3C;
  --gold: #B8862F;
  --transfer: #7A5FA0;
  --loan: #1F7A8C;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Inter', sans-serif;
  background: var(--bg);
  color: var(--ink);
}

.serif { font-family: Georgia, 'Source Serif Pro', serif; }
.num { font-family: 'IBM Plex Mono', 'SFMono-Regular', Consolas, monospace; font-variant-numeric: tabular-nums; }

/* ---------- Login ---------- */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 36px 32px;
  width: 320px;
  text-align: center;
}
.login-card h1 { font-size: 30px; font-weight: 700; color: var(--primary); margin: 0 0 4px; letter-spacing: 0.3px; }
.login-card p { color: var(--muted); font-size: 13px; margin: 0 0 20px; }
.login-card input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 4px;
  font-size: 14px;
  margin-bottom: 12px;
  background: var(--bg);
}
.login-card button {
  width: 100%;
  padding: 10px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 4px;
  font-weight: 600;
  cursor: pointer;
}
.login-error { color: var(--expense); font-size: 13px; margin-top: 8px; min-height: 16px; }

/* ---------- App shell ---------- */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 28px;
  border-bottom: 2px solid var(--primary);
  background: var(--surface);
  flex-wrap: wrap;
  gap: 10px;
}
.app-title { font-size: 26px; color: var(--primary); font-weight: 800; letter-spacing: 0.3px; }
.header-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1.5px solid var(--primary);
  background: var(--surface);
  padding: 9px 16px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--primary);
  text-decoration: none;
  line-height: 1.2;
  transition: background 0.12s, color 0.12s, box-shadow 0.12s;
  box-shadow: 0 1px 2px rgba(27, 67, 50, 0.08);
}
.btn:hover { background: var(--primary); color: white; }
.btn-primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-danger { color: var(--expense); border-color: var(--expense); }
.btn-danger:hover { background: var(--expense); color: white; }

.app-body {
  display: flex;
  align-items: stretch;
  min-height: calc(100vh - 65px);
}

.sidebar {
  width: 220px;
  flex: 0 0 220px;
  background: var(--surface);
  border-right: 1px solid var(--line);
  padding: 18px 0;
}
.tab {
  padding: 12px 24px;
  cursor: pointer;
  font-size: 13.5px;
  color: var(--muted);
  border-left: 3px solid transparent;
  white-space: nowrap;
  font-weight: 500;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.tab:hover { background: var(--bg); color: var(--primary); }
.tab.active { color: var(--primary); border-left-color: var(--primary); background: var(--bg); font-weight: 700; }

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

.view {
  max-width: none;
  margin: 0;
  padding: 28px 32px 64px;
}
.hidden { display: none !important; }

@media (max-width: 720px) {
  .app-body { flex-direction: column; min-height: 0; }
  .sidebar {
    width: 100%; flex: none; display: flex; overflow-x: auto;
    padding: 6px 8px; border-right: none; border-bottom: 1px solid var(--line);
  }
  .tab {
    border-left: none; border-bottom: 3px solid transparent; padding: 10px 14px;
    flex-shrink: 0;
  }
  .tab.active { border-bottom-color: var(--primary); background: none; }
  .view { padding: 22px 18px 56px; }
}

/* ---------- Cards / stats ---------- */
.stat-row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; margin-bottom: 28px; }
.stat-row-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.stat-box { border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 14px 6px; min-width: 0; }
.stat-box-label { font-size: 12.5px; color: var(--muted); margin-bottom: 6px; }
.stat-box-value { font-size: clamp(17px, 4.2vw, 23px); font-weight: 600; white-space: nowrap; overflow-x: auto; scrollbar-width: none; }
.stat-box-value::-webkit-scrollbar { display: none; }

.section-label {
  font-size: 18px; font-weight: 800; color: var(--primary); margin: 28px 0 14px; border-bottom: 3px solid var(--primary);
  padding-bottom: 10px; display: flex; justify-content: space-between; align-items: center;
  gap: 10px; flex-wrap: wrap; letter-spacing: 0.1px;
}

.card { background: var(--surface); border: 1px solid var(--line); border-radius: 4px; padding: 18px; margin-bottom: 20px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 11.5px; color: var(--muted); }
.field input, .field select, .field textarea {
  border: 1px solid var(--line); border-radius: 4px; padding: 8px 10px; font-size: 14px;
  background: var(--bg); color: var(--ink); font-family: inherit;
}
.field-full { grid-column: 1 / -1; }
.type-toggle { display: flex; gap: 8px; margin-bottom: 14px; }
.type-btn {
  flex: 1; padding: 10px 12px; border: 1.5px solid var(--line); background: var(--surface);
  border-radius: 5px; font-size: 14px; font-weight: 600; cursor: pointer; color: var(--muted);
  transition: border-color 0.12s, background 0.12s, color 0.12s;
}
.type-btn:hover { border-color: var(--primary); color: var(--primary); }
.type-btn.active-income { background: var(--income); border-color: var(--income); color: white; }
.type-btn.active-expense { background: var(--expense); border-color: var(--expense); color: white; }
.type-btn.active-transfer { background: var(--transfer); border-color: var(--transfer); color: white; }

/* ---------- Rows / lists ---------- */
.row {
  display: grid; grid-template-columns: 92px minmax(0, 1fr) 130px minmax(105px, 160px) auto;
  gap: 12px; align-items: center; padding: 13px 4px; border-bottom: 1px solid var(--line);
}
.row-head { font-size: 11px; color: var(--muted); border-bottom: 1px solid var(--ink); padding-bottom: 8px; }
.row-date { font-size: 12.5px; color: var(--muted); }
.row-main { display: flex; flex-direction: column; min-width: 0; }
.row-source { font-size: 14.5px; font-weight: 500; overflow-wrap: break-word; }
.row-notes { font-size: 12px; color: var(--muted); margin-top: 2px; overflow-wrap: break-word; }
.row-category { font-size: 12px; color: var(--muted); }
.row-amount { text-align: right; font-size: 15px; font-weight: 600; white-space: nowrap; }
.row-amount.income { color: var(--income); }
.row-amount.expense { color: var(--expense); }
.row-actions { display: flex; gap: 8px; justify-content: flex-end; }
.icon-btn {
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: 5px;
  cursor: pointer;
  color: var(--ink);
  font-size: 12.5px;
  font-weight: 600;
  padding: 6px 11px;
  white-space: nowrap;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.icon-btn:hover { background: var(--expense); color: white; border-color: var(--expense); }

.simple-list-row {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 13px 4px; border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.simple-list-row > div:first-child { min-width: 0; overflow-wrap: break-word; flex: 1 1 auto; }
.chip { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 8px; }

.filter-row { display: flex; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; align-items: center; }
.filter-row > input, .filter-row > select {
  border: 1px solid var(--line); border-radius: 4px; padding: 9px 10px; font-size: 13.5px;
  background: var(--surface); color: var(--ink); flex: 1 1 150px; min-width: 130px;
}

.budget-bar-track { background: var(--bg); border: 1px solid var(--line); border-radius: 4px; height: 8px; overflow: hidden; margin-top: 6px; }
.budget-bar-fill { height: 100%; background: var(--primary); }
.budget-bar-fill.over { background: var(--expense); }

.empty-state { text-align: center; padding: 40px 20px; color: var(--muted); font-size: 14px; }
.error-banner { background: #F6E3DE; border: 1px solid var(--expense); color: var(--expense); padding: 8px 12px; border-radius: 4px; font-size: 13px; margin-bottom: 16px; }

/* ---------- Transactions ---------- */
.flag-star {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--line);
  font-size: 14px;
  padding: 0 4px 0 0;
  vertical-align: middle;
  line-height: 1;
}
.flag-star.flagged { color: var(--gold); }
.flag-star:hover { color: var(--gold); }

.row-amount.transfer { color: var(--transfer); }
.num.transfer { color: var(--transfer); }
.row-amount.loan_in, .row-amount.loan_out { color: var(--loan); }
.num.loan_in, .num.loan_out { color: var(--loan); }

/* ---------- Projects (YouTube-style cards) ---------- */
.project-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  gap: 22px;
  margin-bottom: 28px;
}
.project-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
}
.project-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 28px rgba(27, 67, 50, 0.18);
  border-color: var(--primary);
}
.project-thumb {
  position: relative;
  height: 130px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.project-initial {
  font-family: Georgia, 'Source Serif Pro', serif;
  font-size: 56px;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.92);
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
}
.project-badge, .project-flag-badge {
  position: absolute;
  bottom: 8px;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  background: rgba(0, 0, 0, 0.6);
  padding: 3px 8px;
  border-radius: 4px;
}
.project-badge { right: 8px; }
.project-flag-badge { left: 8px; background: rgba(184, 134, 47, 0.95); }
.project-info { padding: 14px 16px 16px; }
.project-name { font-size: 16px; font-weight: 700; color: var(--ink); overflow-wrap: break-word; }
.project-sub { font-size: 12px; color: var(--muted); margin-top: 3px; overflow-wrap: break-word; }
.project-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}
.project-stats .num { font-size: 12.5px; font-weight: 700; overflow-wrap: anywhere; }
.project-stat-label { font-size: 10.5px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.4px; margin-bottom: 2px; }
.project-card-new {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  min-height: 250px;
  border: 2px dashed var(--line);
  background: var(--bg);
  gap: 4px;
}
.project-card-new:hover { border-color: var(--primary); background: var(--surface); }
.project-new-plus { font-size: 40px; color: var(--primary); line-height: 1; margin-bottom: 6px; }
.project-header {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 22px;
}
.project-header-title { flex: 1 1 200px; min-width: 0; }
.project-title { font-size: 28px; font-weight: 800; color: var(--primary); overflow-wrap: break-word; }
.project-tag { color: var(--loan); border-color: var(--loan); }

/* ---------- Animated modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 30, 25, 0.55);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  opacity: 0;
  transition: opacity 0.2s ease;
}
.modal-overlay.visible { opacity: 1; }
.modal-box {
  background: var(--surface);
  border-radius: 12px;
  padding: 26px;
  width: 92%;
  max-width: 440px;
  max-height: 88vh;
  overflow-y: auto;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.35);
  transform: scale(0.9) translateY(18px);
  opacity: 0;
  transition: transform 0.28s cubic-bezier(0.2, 0.9, 0.3, 1.25), opacity 0.2s ease;
}
.modal-overlay.visible .modal-box { transform: scale(1) translateY(0); opacity: 1; }

.undo-panel {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 1000;
  max-width: 340px;
}
.undo-item {
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 3px solid var(--expense);
  border-radius: 6px;
  padding: 10px 12px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  font-size: 12.5px;
}
.undo-item-text { color: var(--ink); }
.undo-item-actions { display: flex; gap: 6px; flex-shrink: 0; }
@media (max-width: 600px) {
  .undo-panel { left: 16px; right: 16px; bottom: 16px; max-width: none; }
}

.last-entry-banner {
  background: var(--bg);
  border: 1px solid var(--line);
  border-left: 3px solid var(--primary);
  border-radius: 4px;
  padding: 10px 14px;
  font-size: 13px;
  color: var(--ink);
  margin-bottom: 14px;
}
.last-entry-banner strong { color: var(--primary); }

.mini-card-row {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 28px;
}
.mini-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px 14px;
}
.mini-card-title { font-size: 12px; color: var(--muted); font-weight: 600; margin-bottom: 6px; overflow-wrap: break-word; }
.mini-card-value { font-size: 18px; font-weight: 700; color: var(--primary); }
.mini-card-sub { font-size: 11px; color: var(--muted); margin-top: 2px; }
.due-reminder {
  display: inline-block;
  margin-top: 8px;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 3px;
  background: var(--bg);
  color: var(--muted);
  border: 1px solid var(--line);
}
.due-reminder.soon { background: #FBF0DC; color: var(--gold); border-color: var(--gold); }
.due-reminder.urgent { background: #F6E3DE; color: var(--expense); border-color: var(--expense); }

.mode-tag {
  display: inline-block;
  font-size: 10.5px;
  font-weight: 600;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 1px 6px;
  margin-left: 6px;
  vertical-align: middle;
  white-space: nowrap;
}

/* ---------- Excel-style entry grid ---------- */
.grid-scroll { border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.entry-grid {
  border-collapse: collapse;
  width: 100%;
  table-layout: fixed;
  background: var(--surface);
  font-size: 13px;
}
.entry-grid thead th {
  background: var(--primary);
  color: white;
  font-weight: 600;
  font-size: 11.5px;
  text-align: left;
  padding: 10px 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.entry-grid tbody td {
  border-bottom: 1px solid var(--line);
  border-right: 1px solid var(--line);
  padding: 5px;
  vertical-align: middle;
  overflow: hidden;
}
.entry-grid tbody tr:hover { background: var(--bg); }
.entry-grid input, .entry-grid select {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 7px 6px;
  font-size: 12.5px;
  border-radius: 4px;
  color: var(--ink);
  font-family: inherit;
  box-sizing: border-box;
}
.entry-grid input:focus, .entry-grid select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(27, 67, 50, 0.12);
}
.entry-grid input[type="number"] { text-align: right; font-family: 'IBM Plex Mono', monospace; }
.entry-grid input[type="checkbox"] { width: auto; }
.cash-auto-note {
  padding: 9px 8px;
  font-size: 12.5px;
  color: var(--muted);
  font-style: italic;
  border: 1px dashed var(--line);
  border-radius: 4px;
  text-align: center;
}
.entry-grid td .icon-btn {
  box-sizing: border-box;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font-weight: 500;
}
.entry-grid td .icon-btn:hover { background: var(--bg); color: var(--primary); border-color: var(--primary); }
.entry-grid td { text-align: left; }
.entry-grid td:nth-last-child(2), .entry-grid td:last-child { text-align: center; }
.entry-grid select { text-overflow: ellipsis; }

.checklist-table thead th { text-align: center; }
.checklist-table tbody td { text-align: center; }
.checklist-table tbody td:first-child { text-align: left; }

@media (max-width: 600px) {
  .form-grid { grid-template-columns: 1fr; }
  .stat-row { grid-template-columns: 1fr; }
  .row { grid-template-columns: 68px minmax(0, 1fr) minmax(95px, 130px) auto; gap: 8px; }
  .row-category { display: none; }
  .row-amount { font-size: 14px; }
}

/* Let large monetary figures inside cards (credit card owed, loan amounts) wrap or scroll instead of clipping */
.card .num { overflow-wrap: break-word; }
.card > div[style*="font-size:22px"],
.card > div[style*="font-size:20px"] {
  overflow-x: auto;
  scrollbar-width: none;
}
