:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --primary: #0f766e;
  --primary-dark: #115e59;
  --income: #059669;
  --expense: #dc2626;
  --amber: #d97706;
  --danger: #dc2626;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.04);
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tab-h: 62px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #16202e;
    --surface-2: #1e293b;
    --text: #e2e8f0;
    --text-muted: #94a3b8;
    --border: #334155;
    --primary: #14b8a6;
    --primary-dark: #0d9488;
    --income: #34d399;
    --expense: #f87171;
    --amber: #fbbf24;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

/* Ensure the `hidden` attribute wins over class rules that set `display`
   (e.g. .field, .check-field, .btn), so element.hidden actually hides. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

body {
  min-height: 100vh;
  padding-bottom: calc(var(--tab-h) + var(--safe-bottom));
}

h1, h2 { margin: 0; }

/* ---------- Header ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.app-header h1 { font-size: 1.25rem; font-weight: 700; }
.header-title {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.header-title h1 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.month-picker {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.9rem;
  color: var(--text-muted);
}
.month-picker #month-label { min-width: 84px; text-align: center; font-weight: 600; }

.icon-btn {
  border: none;
  background: var(--surface-2);
  color: var(--text);
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
}

/* ---------- Views ---------- */
main { padding: 16px; max-width: 640px; margin: 0 auto; }
.view { animation: fade 0.15s ease; }
/* Extra bottom room so the floating + button never covers the last entry. */
#view-cutoffs, #view-cutoff-detail { padding-bottom: 88px; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.section-title {
  font-size: 0.95rem;
  font-weight: 700;
  margin: 20px 0 10px;
  color: var(--text);
}

/* ---------- Balance card ---------- */
.balance-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}
.balance-row { display: flex; justify-content: space-between; gap: 8px; }
.balance-item { display: flex; flex-direction: column; gap: 4px; flex: 1; text-align: center; }
.balance-label { font-size: 0.72rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.balance-value { font-size: 1.15rem; font-weight: 700; }
.balance-value.income { color: var(--income); }
.balance-value.expense { color: var(--expense); }

/* ---------- Cut-off cards ---------- */
.cutoff-list { list-style: none; margin: 0; padding: 0; }
.cutoff-card {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 34px 14px 16px;
  margin-bottom: 12px;
  cursor: pointer;
}
.cutoff-card:active { background: var(--surface-2); }
.cutoff-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.cutoff-name { font-weight: 700; font-size: 1.05rem; }
.cutoff-date { font-size: 0.8rem; color: var(--text-muted); flex-shrink: 0; }
.cutoff-stats { display: flex; justify-content: space-between; gap: 8px; }
.cutoff-stat { display: flex; flex-direction: column; gap: 3px; flex: 1; }
.cutoff-stat-label {
  font-size: 0.68rem; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.04em;
}
.cutoff-stat-val { font-size: 1rem; font-weight: 700; }
.cutoff-stat-val.income { color: var(--income); }
.cutoff-stat-val.expense { color: var(--expense); }
.cutoff-chevron {
  position: absolute;
  right: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 1.4rem;
  line-height: 1;
}
.cutoff-cash {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
  font-size: 0.8rem;
  color: var(--text-muted);
}
.cutoff-cash-val { color: var(--income); font-weight: 700; }
.cutoff-cash-note { color: var(--text-muted); margin-left: 4px; }

/* Bank + cash balances shown side by side (never summed) */
.cutoff-balances { display: flex; gap: 10px; }
.cutoff-bal { flex: 1; display: flex; flex-direction: column; gap: 3px; }
.cutoff-bal-label {
  font-size: 0.68rem; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.03em;
}
.cutoff-bal-val { font-size: 1.15rem; font-weight: 700; }
.cutoff-flow {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
  font-size: 0.78rem;
  color: var(--text-muted);
}

/* ---------- Cash-on-hand card (detail view) ---------- */
.cash-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  margin-bottom: 16px;
}
.cash-title {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.cash-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
  padding: 3px 0;
}
.cash-row span:last-child { font-weight: 600; }
.cash-balance { font-size: 1.05rem; padding: 6px 0; }
.cash-balance span:last-child { font-weight: 700; }
.cash-total {
  margin-top: 6px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-weight: 700;
}
.cash-total span:last-child { font-weight: 700; }
.cash-note {
  margin-top: 8px;
  font-size: 0.78rem;
  color: var(--text-muted);
}

/* ---------- Transaction list ---------- */
.tx-list, .budget-list, .category-list { list-style: none; margin: 0; padding: 0; }

.tx-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  margin-bottom: 10px;
  cursor: pointer;
}
.tx-main { flex: 1; min-width: 0; }
.tx-category { font-weight: 600; }
.tx-note { font-size: 0.8rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-right { text-align: right; flex-shrink: 0; }
.tx-amount { font-weight: 700; }
.tx-amount.income { color: var(--income); }
.tx-amount.expense { color: var(--expense); }
.tx-amount.withdrawal { color: var(--amber); }
.tx-date { font-size: 0.75rem; color: var(--text-muted); }
.tx-badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 999px;
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  vertical-align: middle;
}
.tx-badge.cash { background: color-mix(in srgb, var(--amber) 20%, transparent); color: var(--amber); }

.empty-state { text-align: center; color: var(--text-muted); margin-top: 40px; }

/* ---------- Summary ---------- */
.summary-cards { display: flex; gap: 12px; margin-bottom: 12px; }
.summary-card {
  flex: 1;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  display: flex; flex-direction: column; gap: 6px;
}
.summary-label { font-size: 0.78rem; color: var(--text-muted); }
.summary-value { font-size: 1.4rem; font-weight: 700; }
.summary-value.income { color: var(--income); }
.summary-value.expense { color: var(--expense); }
.summary-note { font-size: 0.8rem; color: var(--text-muted); margin-top: 2px; }
.chart {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  overflow-x: auto;
}
.chart svg { display: block; width: 100%; height: auto; }
.chart-empty { color: var(--text-muted); text-align: center; padding: 20px 0; }
.chart-legend { display: flex; gap: 16px; font-size: 0.8rem; margin-top: 8px; }
.legend-item { display: inline-flex; align-items: center; }
.legend-swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; }
.legend-swatch.income { background: #10b981; }
.legend-swatch.expense { background: #ef4444; }

/* ---------- Settings ---------- */
.settings-group {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 14px;
}
.settings-head { margin-bottom: 14px; }
.settings-title { font-size: 1rem; font-weight: 700; }
.settings-desc { margin: 6px 0 0; font-size: 0.82rem; color: var(--text-muted); line-height: 1.45; }

.settings-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 0;
  border-top: 1px solid var(--border);
  font-size: 0.9rem;
}
.settings-row-label { color: var(--text-muted); }
.settings-row-value { font-weight: 600; }

.settings-actions { display: flex; gap: 10px; margin-top: 12px; }
.settings-actions .btn { margin-top: 0; }

.settings-danger { border: 1px solid color-mix(in srgb, var(--danger) 35%, var(--border)); }

.app-version { text-align: center; font-size: 0.75rem; color: var(--text-muted); margin-top: 4px; }

.install-steps { margin: 0; padding-left: 20px; }
.install-steps li { font-size: 0.85rem; color: var(--text); line-height: 1.5; margin-bottom: 6px; }
.install-steps li:last-child { margin-bottom: 0; }

.add-category-row { display: flex; gap: 8px; margin-bottom: 14px; }
.add-category-row input {
  flex: 1;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 1rem;
}
.btn.btn-inline { width: auto; padding: 11px 18px; margin-top: 0; flex-shrink: 0; }

.category-list li {
  display: flex; justify-content: space-between; align-items: center;
  padding: 11px 12px;
  margin-bottom: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
}
.category-list li:last-child { margin-bottom: 0; }
.category-name { font-weight: 600; font-size: 0.92rem; }
.cat-del {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 6px;
}

/* ---------- Buttons ---------- */
.btn {
  display: block;
  width: 100%;
  padding: 13px;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  margin-top: 10px;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn-danger-solid { background: var(--danger); color: #fff; }

/* ---------- Confirm modal ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 50; }
.modal {
  position: fixed;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  z-index: 51;
  width: min(90vw, 360px);
  background: var(--surface);
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.35);
  animation: pop 0.15s ease;
}
@keyframes pop {
  from { transform: translate(-50%, -50%) scale(0.96); opacity: 0; }
  to { transform: translate(-50%, -50%) scale(1); opacity: 1; }
}
.modal-title { font-size: 1.1rem; font-weight: 700; margin-bottom: 8px; }
.modal-message { font-size: 0.9rem; color: var(--text-muted); line-height: 1.5; margin: 0 0 18px; }
.modal-actions { display: flex; gap: 10px; }
.modal-actions .btn { margin-top: 0; }

/* ---------- FAB ---------- */
.fab {
  position: fixed;
  right: 18px;
  bottom: calc(var(--tab-h) + var(--safe-bottom) + 14px);
  width: 56px; height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  border: none;
  font-size: 2rem;
  line-height: 1;
  box-shadow: 0 4px 12px rgba(0,0,0,0.25);
  cursor: pointer;
  z-index: 20;
}
.fab:active { background: var(--primary-dark); }

/* ---------- Tab bar ---------- */
.tab-bar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--tab-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 15;
}
.tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px;
  font-size: 0.7rem;
  cursor: pointer;
}
.tab.active { color: var(--primary); }
.tab-icon { line-height: 0; }
.tab-icon svg { width: 24px; height: 24px; display: block; }

/* ---------- Bottom sheet ---------- */
.sheet-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 30;
}
.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 31;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 8px 18px calc(18px + var(--safe-bottom));
  box-shadow: 0 -4px 20px rgba(0,0,0,0.2);
  max-height: 90vh;
  overflow-y: auto;
  animation: slideup 0.2s ease;
}
@keyframes slideup { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-handle { width: 40px; height: 4px; background: var(--border); border-radius: 999px; margin: 6px auto 12px; }
.sheet h2 { font-size: 1.1rem; margin-bottom: 14px; }

.type-toggle { display: flex; gap: 8px; margin-bottom: 14px; }
.type-option {
  flex: 1;
  position: relative;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  text-align: center;
  cursor: pointer;
}
.type-option input { position: absolute; opacity: 0; }
.type-option:has(input:checked) { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 12%, transparent); color: var(--primary); font-weight: 600; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; font-size: 0.85rem; color: var(--text-muted); }
.field input, .field select {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 1rem;
}
.check-field {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
  font-size: 0.9rem;
  color: var(--text);
  cursor: pointer;
}
.check-field input { width: 20px; height: 20px; flex-shrink: 0; accent-color: var(--primary); }

.field-hint {
  margin: -4px 0 12px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.sheet-actions { display: flex; gap: 8px; margin-top: 8px; }
.sheet-actions .btn { margin-top: 0; }

/* ---------- Banner ---------- */
.banner {
  position: sticky;
  top: 0;
  z-index: 25;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(var(--safe-top) + 10px) 14px 10px;
  background: var(--amber);
  color: #fff;
  font-size: 0.85rem;
}
.banner-btn { background: rgba(255,255,255,0.25); color: #fff; border: none; padding: 6px 12px; border-radius: 8px; font-weight: 600; cursor: pointer; }
.banner-close { margin-left: auto; background: none; border: none; color: #fff; font-size: 1.3rem; cursor: pointer; line-height: 1; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tab-h) + var(--safe-bottom) + 20px);
  transform: translateX(-50%);
  background: var(--text);
  color: var(--surface);
  padding: 12px 18px;
  border-radius: 999px;
  font-size: 0.85rem;
  z-index: 40;
  box-shadow: var(--shadow);
  animation: fade 0.2s ease;
}
