:root {
  --paper: #F5F7FB;
  --paper-card: #FFFFFF;
  --ink: #1E293B;
  --ink-soft: #64748B;
  --rule: #E2E8F0;
  --ledger-green: #16A34A;
  --ledger-green-dark: #0F7A38;
  --brass: #F59E0B;
  --brick: #E11D48;
  --brick-soft: #FCE7EC;
  --green-soft: #DCFCE7;
  --accent-blue: #2563EB;
  --accent-blue-soft: #DBEAFE;
  --accent-orange: #EA580C;
  --accent-orange-soft: #FFEDD5;
  --accent-purple: #7C3AED;
  --accent-purple-soft: #EDE4FD;
  --accent-teal: #0D9488;
  --accent-teal-soft: #D9F5F1;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

h1, h2, h3 {
  font-family: "Source Serif 4", Georgia, serif;
  font-weight: 600;
  margin: 0 0 4px 0;
  color: var(--ink);
}

.mono, .amount, table td.amount, .stat-value {
  font-family: "IBM Plex Mono", monospace;
  font-variant-numeric: tabular-nums;
}

a { color: var(--accent-blue); text-decoration: none; }
a:hover { text-decoration: underline; }

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

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: linear-gradient(165deg, #0F766E 0%, #15803D 55%, #166534 100%);
  color: #ECFDF5;
  padding: 24px 0;
  display: flex;
  flex-direction: column;
}

.sidebar .brand {
  font-family: "Source Serif 4", serif;
  font-size: 22px;
  color: #fff;
  padding: 0 24px 20px 24px;
  border-bottom: 1px solid rgba(255,255,255,0.18);
  margin-bottom: 12px;
}
.sidebar .brand span { color: var(--brass); }

.sidebar nav a {
  display: block;
  color: #D1FAE5;
  padding: 10px 24px;
  font-size: 14px;
  border-left: 3px solid transparent;
  transition: background 0.15s ease, color 0.15s ease;
}
.sidebar nav a:hover { text-decoration: none; background: rgba(255,255,255,0.1); color: #fff; }
.sidebar nav a.active {
  color: #fff;
  background: rgba(255,255,255,0.16);
  border-left-color: var(--brass);
  font-weight: 600;
}

.main { flex: 1; padding: 32px 40px; max-width: 1500px; }

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}
.page-header h1 { font-size: 26px; }
.page-header .sub { color: var(--ink-soft); font-size: 14px; }

.card {
  background: var(--paper-card);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.04);
  transition: box-shadow 0.15s ease;
}

.card-title {
  font-family: "Source Serif 4", serif;
  font-size: 17px;
  margin-bottom: 14px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--rule);
}

.grid { display: grid; gap: 20px; }
.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.forecast-page { display: flex; flex-direction: column; height: calc(100vh - 64px); min-height: 420px; }
.forecast-layout { display: flex; gap: 20px; flex: 1; min-height: 0; }
.forecast-card {
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  background: var(--paper-card);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.04);
}
.forecast-layout .forecast-card:first-child { flex: 3 1 0; }
.forecast-layout .forecast-card:last-child { flex: 1.1 1 0; min-width: 280px; }
.forecast-card-body { flex: 1; min-height: 0; position: relative; }
.forecast-card-body.scrollable { overflow-y: auto; border: 1px solid var(--rule); border-radius: var(--radius); }

.stat-row { display: flex; gap: 20px; flex-wrap: wrap; margin-bottom: 20px; }
.stat-box {
  flex: 1;
  min-width: 160px;
  background: var(--paper-card);
  border: 1px solid var(--rule);
  border-left: 4px solid var(--accent-blue);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.04);
}
.stat-row .stat-box:nth-child(4n+1) { border-left-color: var(--accent-blue); }
.stat-row .stat-box:nth-child(4n+2) { border-left-color: var(--accent-orange); }
.stat-row .stat-box:nth-child(4n+3) { border-left-color: var(--accent-purple); }
.stat-row .stat-box:nth-child(4n+4) { border-left-color: var(--accent-teal); }
.stat-label { font-size: 12.5px; color: var(--ink-soft); margin-bottom: 6px; }
.stat-value { font-size: 22px; font-weight: 600; }
.stat-value.positive { color: var(--ledger-green); }
.stat-value.negative { color: var(--brick); }

table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  font-size: 12px;
  text-transform: none;
  color: var(--ink-soft);
  font-weight: 600;
  padding: 8px 10px;
  border-bottom: 1px solid var(--rule);
}
td { padding: 9px 10px; border-bottom: 1px solid var(--rule); font-size: 14px; vertical-align: middle; }
tr { transition: background 0.1s ease; }
tbody tr:hover, table tr:hover td { background: rgba(37, 99, 235, 0.035); }
tr:last-child td { border-bottom: none; }
td.amount { text-align: right; white-space: nowrap; }
.amount.positive { color: var(--ledger-green); }
.amount.negative { color: var(--brick); }

.btn {
  display: inline-block;
  background: var(--ledger-green);
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.08);
  transition: background 0.15s ease, transform 0.1s ease;
}
.btn:hover { background: var(--ledger-green-dark); text-decoration: none; transform: translateY(-1px); }
.btn-outline {
  background: transparent;
  border: 1px solid var(--rule);
  color: var(--ink);
}
.btn-outline:hover { background: var(--accent-blue-soft); border-color: var(--accent-blue); }
.btn-outline.active { background: var(--accent-blue); color: #fff; border-color: var(--accent-blue); }
.btn-small { padding: 4px 10px; font-size: 12.5px; }
.btn-danger { background: var(--brick); }
.btn-danger:hover { background: #B0123A; }
.btn-link { background: none; border: none; color: var(--accent-blue); cursor: pointer; font-size: 13px; padding: 0; }

form.inline { display: inline; }

input, select, textarea {
  font-family: inherit;
  font-size: 14px;
  padding: 8px 10px;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brass); outline-offset: 0; border-color: var(--brass); }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; color: var(--ink-soft); margin-bottom: 5px; }
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }

.flash {
  padding: 10px 16px;
  border-radius: var(--radius);
  margin-bottom: 18px;
  font-size: 14px;
}
.flash.success { background: var(--green-soft); color: var(--ledger-green-dark); border: 1px solid #A7F3C6; }
.flash.error { background: var(--brick-soft); color: var(--brick); border: 1px solid #F9C4D2; }

.badge {
  display: inline-block;
  font-size: 11.5px;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--green-soft);
  color: var(--ledger-green-dark);
}
.badge.due-soon { background: var(--accent-orange-soft); color: var(--accent-orange); }
.badge.muted { background: #EEF2F7; color: var(--ink-soft); }

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

.progress-track {
  background: #E9EDF3;
  border-radius: 10px;
  height: 8px;
  overflow: hidden;
  margin-top: 6px;
}
.progress-fill { height: 100%; background: var(--ledger-green); }
.progress-fill.over { background: var(--brick); }

.month-nav { display: flex; align-items: center; gap: 12px; }
.month-nav .current { font-weight: 600; min-width: 90px; text-align: center; font-family: "IBM Plex Mono", monospace; }

.tabs { display: flex; gap: 4px; margin-bottom: 18px; border-bottom: 1px solid var(--rule); }
.tabs a { padding: 8px 14px; font-size: 14px; color: var(--ink-soft); border-bottom: 2px solid transparent; }
.tabs a.active { color: var(--ink); border-bottom-color: var(--brass); font-weight: 600; }

.checkbox-cell { text-align: center; width: 30px; }

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

.sort-arrows { display: inline-flex; flex-direction: column; margin-left: 6px; vertical-align: middle; line-height: 1; gap: 1px; }
.sort-arrow { cursor: pointer; font-size: 8px; color: var(--rule); user-select: none; }
.sort-arrow:hover { color: var(--accent-blue); }
.sort-arrow-active { color: var(--ledger-green); }
th:has(.sort-arrows) { cursor: default; }

/* ---------- Compact density (Settings > Formatting) ---------- */
body.density-compact { font-size: 13px; line-height: 1.35; }
body.density-compact h1 { font-size: 20px; }
body.density-compact h2, body.density-compact h3 { font-size: 15px; }
body.density-compact .page-header { margin-bottom: 14px; }
body.density-compact .page-header h1 { font-size: 19px; }
body.density-compact .page-header .sub { font-size: 12px; }
body.density-compact .main { padding: 22px 26px; }
body.density-compact .card, body.density-compact .forecast-card { padding: 12px; margin-bottom: 12px; }
body.density-compact .card-title { font-size: 14px; margin-bottom: 8px; padding-bottom: 6px; }
body.density-compact .grid { gap: 12px; }
body.density-compact .stat-row { gap: 10px; margin-bottom: 12px; }
body.density-compact .stat-box { padding: 8px 10px; }
body.density-compact .stat-label { font-size: 11px; margin-bottom: 3px; }
body.density-compact .stat-value { font-size: 16px; }
body.density-compact th { font-size: 10.5px; padding: 4px 6px; }
body.density-compact td { font-size: 12px; padding: 4px 6px; }
body.density-compact .btn { padding: 5px 10px; font-size: 12px; }
body.density-compact .btn-small { padding: 2px 6px; font-size: 10.5px; }
body.density-compact input, body.density-compact select, body.density-compact textarea { padding: 5px 7px; font-size: 12px; }
body.density-compact .field { margin-bottom: 8px; }
body.density-compact .field label { font-size: 10.5px; margin-bottom: 2px; }
body.density-compact .field-row { gap: 8px; }
body.density-compact .badge { font-size: 10px; padding: 1px 6px; }
body.density-compact .sidebar { padding: 16px 0; }
body.density-compact .sidebar .brand { font-size: 18px; padding-bottom: 12px; margin-bottom: 8px; }
body.density-compact .sidebar nav a { padding: 6px 24px; font-size: 12.5px; }
body.density-compact .empty-state { padding: 20px 10px; }
body.density-compact .flash { padding: 6px 12px; font-size: 12px; margin-bottom: 12px; }
body.density-compact .month-nav .current { min-width: 70px; }

@media (max-width: 800px) {
  .app-shell { flex-direction: column; }
  .sidebar { width: 100%; padding: 10px 0; }
  .sidebar .brand { display: none; }
  .sidebar nav { display: flex; overflow-x: auto; gap: 4px; padding: 0 12px; -webkit-overflow-scrolling: touch; }
  .sidebar nav a { display: inline-block; white-space: nowrap; padding: 8px 14px; border-left: none; border-radius: 999px; }
  .sidebar nav a.active { border-left: none; }
  .main { padding: 16px; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .forecast-page { height: auto; }
  .forecast-layout { flex-direction: column; flex: none; }
  .forecast-layout .forecast-card,
  .forecast-layout .forecast-card:first-child,
  .forecast-layout .forecast-card:last-child { flex: none; min-width: 0; }
  .forecast-layout .forecast-card-body { min-height: 320px; }
  .field-row { flex-direction: column; gap: 0; }
  .field-row .field { max-width: none !important; }
  .stat-row { gap: 12px; }
  .table-scroll table { min-width: 560px; }
}

@media (max-width: 480px) {
  .page-header h1 { font-size: 21px; }
  .card, .forecast-card { padding: 14px; }
  .stat-box { min-width: 45%; }
}
