:root {
  --bg: #f1f5f4;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #0f766e;
  --primary-dark: #115e59;
  --danger: #dc2626;
  --border: #e2e8f0;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  padding-bottom: env(safe-area-inset-bottom);
}

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px 14px calc(90px + env(safe-area-inset-bottom));
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.06);
  padding: 16px;
  margin-bottom: 14px;
}

.muted { color: var(--muted); }
.small { font-size: 12px; }
.hidden { display: none !important; }

/* Summary */
.summary-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.total-label { display: block; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 2px; opacity: 0.9; }
.total { font-size: 34px; font-weight: 700; letter-spacing: -0.5px; margin: 0 0 12px; }
.currency-btn {
  border: none; background: #ecfdf5; color: var(--primary-dark);
  font-size: 18px; font-weight: 700; padding: 4px 10px; border-radius: 10px; cursor: pointer;
}
.summary-row { display: flex; gap: 10px; }
.month-box, .count-box {
  flex: 1; background: #f8fafc; border-radius: 10px; padding: 10px 12px;
  display: flex; flex-direction: column; gap: 2px;
}

/* This-month chart */
.chart-head { display: flex; align-items: center; gap: 4px; margin-bottom: 10px; }
.chart-head h2 { font-size: 16px; margin: 0; flex: 1; text-align: center; }
.month-nav {
  border: none; background: transparent; color: var(--muted);
  font-size: 24px; line-height: 1; width: 38px; height: 34px; padding: 0;
  border-radius: 9px; cursor: pointer; flex: none;
  display: inline-flex; align-items: center; justify-content: center;
}

/* Lucide icons (ISC license, lucide.dev) — sized in em so they follow each button's font-size */
svg.icon { width: 1.05em; height: 1.05em; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.btn-primary svg.icon, .btn-secondary svg.icon { margin-right: .45em; }
.link-btn svg.icon { width: 1em; height: 1em; margin-right: .35em; vertical-align: -0.12em; }
.month-nav:hover { background: #eef2f7; color: var(--text); }
.month-nav:disabled { opacity: .3; cursor: default; background: transparent; }
.chart-body { display: flex; flex-direction: column; gap: 10px; }
/* Horizontal stacked bar above the legend */
.hbar-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-size: 13px; color: var(--muted); }
.hbar-head strong { font-size: 15px; color: var(--text); font-variant-numeric: tabular-nums; }
.hbar { display: flex; height: 14px; border-radius: 999px; overflow: hidden; background: #e2e8f0; }
.hbar > div { height: 100%; flex: none; }
.legend { list-style: none; margin: 0; padding: 0; }
.legend li { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.legend li:last-child { border-bottom: none; }
.legend .cat-name { font-size: 14px; display: flex; align-items: center; gap: 8px; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.legend .cat-val { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.legend .pct { color: var(--muted); font-weight: 500; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }

/* Category chips (used categories, quick pick) */
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.chip {
  border: 1px solid var(--border); background: #f8fafc; color: var(--text);
  font-size: 13.5px; font-weight: 600; padding: 6px 12px; border-radius: 999px;
  cursor: pointer; display: inline-flex; align-items: center; gap: 7px; transition: background .12s, border-color .12s;
}
.chip .dot { width: 8px; height: 8px; }
.chip.active { background: #ecfdf5; border-color: var(--primary); color: var(--primary-dark); }

/* Form */
.field { display: block; margin-bottom: 12px; }
.field span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; font-weight: 600; }
input, select {
  width: 100%; padding: 12px; font-size: 16px; border: 1px solid var(--border);
  border-radius: 10px; background: #fff; color: var(--text); -webkit-appearance: none; appearance: none;
}
input:focus, select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(15,118,110,0.12); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.btn-primary {
  width: 100%; border: none; background: var(--primary); color: #fff; font-size: 16px; font-weight: 700;
  padding: 13px; border-radius: 12px; cursor: pointer; transition: background .15s;
}
.btn-primary:active { background: var(--primary-dark); }

.btn-secondary {
  width: 100%; border: 1px solid var(--primary); background: #fff; color: var(--primary-dark);
  font-size: 16px; font-weight: 700; padding: 13px; border-radius: 12px; cursor: pointer; transition: background .15s;
}
.btn-secondary:hover { background: #f0fdfa; }
.btn-secondary:active { background: #ccfbf1; }
.small-btn { width: auto; padding: 8px 14px; font-size: 14px; border-radius: 10px; }

.btn-danger {
  width: 100%; border: 1px solid var(--danger); background: #fff; color: var(--danger);
  font-size: 16px; font-weight: 700; padding: 13px; border-radius: 12px; cursor: pointer; transition: background .15s;
}
.btn-danger:hover { background: #fef2f2; }
.btn-danger:active { background: #fee2e2; }

/* List */
.list-head { display: flex; align-items: center; justify-content: space-between; margin: 6px 2px 10px; }
.list-head h2 { font-size: 18px; margin: 0; }
.filters select { width: auto; padding: 8px 30px 8px 10px; font-size: 14px; border-radius: 10px; background-image: none; }

.list { list-style: none; margin: 0; padding: 6px 0; }
.item { display: flex; align-items: center; gap: 12px; padding: 12px 10px; border-bottom: 1px solid var(--border); }
.item:last-child { border-bottom: none; }
.item .cat-dot { width: 34px; height: 34px; border-radius: 10px; flex: none; display: grid; place-items: center; font-weight: 700; color: #fff; font-size: 15px; }
.item .info { flex: 1; min-width: 0; }
.item .desc { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .meta { font-size: 12.5px; color: var(--muted); display: flex; gap: 8px; align-items: center; margin-top: 1px; }
.item .amount { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
/* Monthly budget (inside summary card) */
/* Top bar + settings page */
.topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.topbar h1, .page-head h1 { font-size: 20px; margin: 0; }
.page-head { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; }
.page-head h1 { flex: 1; }
.row-inline { display: flex; gap: 8px; margin-top: 10px; }
.row-inline input { flex: 1 1 0; min-width: 0; height: 42px; width: auto; }
#currencySave, #newCatAdd { flex: none; width: auto; padding: 10px 20px; }

/* Settings: monthly plan */
.card h3 { font-size: 14.5px; margin: 16px 0 2px; }
.inc-main { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1; }
.inc-main > div { flex: 1; min-width: 0; }
.inc-main strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.inc-amt { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; margin-left: auto; }
#incomeList .inc-name-inp { flex: 1 1 auto; min-width: 0; }
#incomeList .inc-amt-inp { flex: none; width: 110px !important; }
#incAdd, #savingsSave { flex: none; width: auto; padding: 10px 16px; }
#incAmountInput, #savingsInput { max-width: 120px; flex: none; }

.cat-admin { list-style: none; margin: 12px 0 4px; padding: 0; }
.cat-row { display: flex; justify-content: space-between; align-items: center; gap: 6px; padding: 9px 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }

/* Collapsed section for dated (one-off) incomes in the monthly plan */
.oneoff-summary { cursor: pointer; user-select: none; font-size: 13.5px; padding: 9px 2px; list-style-position: inside; }
.cat-main { display: flex; align-items: center; gap: 9px; min-width: 0; flex: 1; }
.cat-main > div { min-width: 0; }
.cat-main strong { display: block; font-size: 15px; }
.cat-menu-btn { font-size: 19px; padding: 5px; }

/* Category dropdown (settings) */
.cat-row { position: relative; }
.cat-menu {
  position: absolute; top: calc(100% - 4px); right: 2px; z-index: 40;
  min-width: 185px; background: var(--card);
  border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, .16); padding: 6px;
}
.cat-menu .menu-item {
  display: block; width: 100%; text-align: left;
  font-size: 14.5px; font-weight: 600; color: var(--text);
  background: transparent; border: none; border-radius: 8px;
  padding: 9px 12px; cursor: pointer; line-height: 1.3;
}
.cat-menu .menu-item:hover { background: #f1f5f9; }
.cat-menu .menu-item.danger { color: var(--danger); }
.cat-menu .menu-item.danger:hover { background: #fee2e2; }
.merge-targets { width: 100%; padding-top: 4px; }
.merge-btns { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.mini-btn { border: none; border-radius: 8px; padding: 5px 10px; font-size: 13px; cursor: pointer; margin-left: 5px; }
.mini-btn.ok { background: #ccfbf1; color: var(--primary-dark); }
.mini-btn.no { background: #fee2e2; color: #b91c1c; }
.rename-input { flex: 1 1 0; min-width: 0; width: auto; padding: 8px 10px !important; font-size: 15px !important; }

/* Category field header with ＋ add button */
/* .field span has higher specificity, so scope the head explicitly */
.field .field-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.field .field-head > span:first-child { margin-bottom: 0; }
.chip-add {
  width: 26px; height: 26px; padding: 0; flex: none;
  border: 1.5px dashed var(--primary); color: var(--primary); background: #fff;
  border-radius: 999px; font-size: 16px; line-height: 1; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.chip-add:hover { background: #ccfbf1; }

.budget-box { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--border); }
.budget-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.link-btn { border: none; background: transparent; color: var(--primary); font-size: 13px; font-weight: 600; cursor: pointer; padding: 2px 4px; border-radius: 6px; flex: none; }
.link-btn:hover { text-decoration: underline; }
/* Monthly plan rows: one column, label left / value right (readable on mobile and desktop) */
.plan-grid { display: grid; grid-template-columns: 1fr; gap: 8px; margin-top: 6px; }
.plan-grid > div { background: #f8fafc; border-radius: 10px; padding: 9px 12px; display: flex; align-items: baseline; justify-content: space-between; gap: 8px; min-width: 0; }
.budget-box strong { display: block; margin-top: 2px; font-size: 18px; }
#budgetTip { display: block; color: var(--muted); font-weight: 500; }
/* Status card: same look as the plan rows; over-limit shows two lines (red = over, green = actually saved) */
#budgetStatus { background: #f8fafc; border-radius: 10px; padding: 9px 12px; }
#budgetStatus .st-line { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; min-width: 0; }
#budgetStatus .st-line + .st-line { margin-top: 4px; }
#budgetStatus .st-over { color: #f87171; font-weight: 500; }
#budgetStatus .st-saved { color: var(--primary-dark); font-weight: 600; }
#budgetStatus.ok { color: var(--primary-dark); font-weight: 600; }
#budgetStatus.warn { color: #b45309; font-weight: 600; }
#budgetStatus.over { color: #f87171; font-weight: 500; }

.icon-btn { border: none; background: transparent; color: var(--muted); font-size: 20px; line-height: 1; cursor: pointer; padding: 6px; border-radius: 8px; flex: none; display: inline-flex; align-items: center; justify-content: center; }
.icon-btn:hover { background: #e2e8f0; color: var(--text); }
.icon-btn.danger:hover { background: #fee2e2; color: var(--danger); }

.empty { text-align: center; margin: 24px 0; }

/* Install banner */
.install-banner {
  position: fixed; left: 12px; right: 12px; bottom: calc(10px + env(safe-area-inset-bottom));
  max-width: 560px; margin: 0 auto; background: #1e293b; color: #fff; border-radius: 14px;
  padding: 12px 14px; display: flex; align-items: center; gap: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.25); z-index: 50;
}
.install-banner span { flex: 1; font-size: 13.5px; }

/* Inline plan editor (below the "This month" summary) */
.plan-edit { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--border, #cbd5e1); }
.pe-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; font-size: 13.5px; }
.pe-done, .pe-reset, .pe-link { font-size: 12.5px; }
.pe-reset { display: block; padding: 0; margin-top: 6px; text-align: left; }
.pe-h { margin: 14px 0 6px; font-size: 12.5px; letter-spacing: .4px; opacity: .85; }
