:root {
  --bs-red: #E60012;
  --bs-red-dark: #B91C1C;
  --green: #0a7d33;
  --green-bg: #e8f5ec;
  --red-bg: #fdecec;
  --ink: #1a1a1a;
  --muted: #6b7280;
  --line: #e2e5ea;
  --bg: #f4f5f7;
  --card: #ffffff;
  font-family: -apple-system, "Segoe UI", Tahoma, "Leelawadee UI", sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); }

/* header */
.app-header {
  background: linear-gradient(90deg, var(--bs-red), var(--bs-red-dark));
  color: #fff; padding: 14px 20px;
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px;
}
.brand-text h1 { margin: 0; font-size: 1.3rem; }
.subtitle { margin: 2px 0 0; font-size: .78rem; opacity: .9; }
.period { display: flex; gap: 6px; align-items: center; }
.period select {
  padding: 6px 8px; border-radius: 6px; border: none; font-size: .9rem; min-width: 76px;
}
.btn-nav {
  width: 32px; height: 32px; border: none; border-radius: 6px; cursor: pointer;
  background: rgba(255,255,255,.25); color: #fff; font-size: 1.1rem; line-height: 1;
}
.btn-nav:hover { background: rgba(255,255,255,.4); }

.container { max-width: 1000px; margin: 18px auto; padding: 0 16px; }

/* summary */
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 16px; }
.sum-card {
  background: var(--card); border-radius: 10px; padding: 14px 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,.08); display: flex; flex-direction: column; gap: 4px;
  border-left: 5px solid var(--muted);
}
.sum-card.income { border-left-color: var(--green); }
.sum-card.expense { border-left-color: var(--bs-red); }
.sum-card.net { border-left-color: #2563eb; }
.sum-label { font-size: .8rem; color: var(--muted); }
.sum-value { font-size: 1.5rem; font-weight: 700; }
.sum-card.net .sum-value.neg { color: var(--bs-red); }

.columns { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

.card {
  background: var(--card); border-radius: 10px; padding: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,.08); margin-bottom: 16px;
}
.card h2 { margin: 0 0 12px; font-size: 1.05rem; }
.head-income { color: var(--green); }
.head-expense { color: var(--bs-red); }

.add-row { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.add-row input { padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; font-size: .9rem; }
.add-row input[type=number] { width: 110px; text-align: right; }
.grow { flex: 1; min-width: 120px; }

button { font-family: inherit; }
.btn-primary { background: var(--bs-red); color: #fff; border: none; padding: 8px 14px; border-radius: 6px; cursor: pointer; font-size: .9rem; }
.btn-primary:hover { background: var(--bs-red-dark); }
.btn-secondary { background: #fff; color: var(--ink); border: 1px solid var(--line); padding: 8px 14px; border-radius: 6px; cursor: pointer; font-size: .88rem; }
.btn-secondary:hover { background: #f0f1f3; }
.btn-danger { background: #fff; color: var(--bs-red); border: 1px solid var(--bs-red); padding: 8px 14px; border-radius: 6px; cursor: pointer; font-size: .88rem; }
.btn-danger:hover { background: var(--red-bg); }
.file-label { display: inline-flex; align-items: center; }

/* table */
.tbl { width: 100%; border-collapse: collapse; font-size: .9rem; }
.tbl th, .tbl td { padding: 7px 8px; border-bottom: 1px solid var(--line); text-align: left; }
.tbl th { font-weight: 600; color: var(--muted); font-size: .8rem; }
.tbl .num { text-align: right; }
.tbl tfoot td { font-weight: 700; border-top: 2px solid var(--line); border-bottom: none; }
.del-btn { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 1rem; }
.del-btn:hover { color: var(--bs-red); }
.empty { color: var(--muted); font-style: italic; }

.actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.saved-at { color: var(--muted); font-size: .8rem; margin-left: auto; }
.hint { color: var(--muted); font-size: .8rem; }

.app-footer { text-align: right; padding: 10px 20px; color: var(--muted); font-size: .75rem; }

@media (max-width: 720px) {
  .columns { grid-template-columns: 1fr; }
  .summary { grid-template-columns: 1fr; }
}

/* tabs */
.tabs { max-width: 1000px; margin: 14px auto 0; padding: 0 16px; display: flex; gap: 6px; }
.tab {
  background: transparent; border: none; border-bottom: 3px solid transparent;
  padding: 8px 16px; cursor: pointer; font-size: .95rem; color: var(--muted);
}
.tab.active { color: var(--bs-red); border-bottom-color: var(--bs-red); font-weight: 600; }

/* carry bar */
.carry-bar { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.btn-secondary:disabled { opacity: .5; cursor: not-allowed; }

/* year table */
.year-tbl .num.neg { color: var(--bs-red); }
.year-tbl tfoot td { font-size: 1rem; }
.month-link { color: var(--ink); text-decoration: none; border-bottom: 1px dashed var(--muted); }
.month-link:hover { color: var(--bs-red); }
.row-empty { opacity: .5; }
.sum-value.neg { color: var(--bs-red); }

/* modal */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 50;
}
.modal-box {
  background: var(--card); border-radius: 10px; padding: 18px; width: 100%; max-width: 560px;
  max-height: 85vh; overflow: auto; box-shadow: 0 10px 40px rgba(0,0,0,.25);
}
.modal-box h3 { margin: 0 0 6px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 14px; }
#tplKind { border: 1px solid var(--line); padding: 8px; border-radius: 6px; }
input[type=color] { width: 40px; height: 36px; padding: 2px; border: 1px solid var(--line); border-radius: 6px; cursor: pointer; background: #fff; }
.tbl td input[type=color] { height: 28px; width: 34px; }

/* template permanent rows */
.tpl-badge {
  display: inline-block; font-size: .65rem; background: var(--muted); color: #fff;
  padding: 1px 6px; border-radius: 8px; vertical-align: middle; margin-right: 4px;
}
.val-input {
  width: 110px; text-align: right; padding: 5px 7px;
  border: 1px solid var(--line); border-radius: 6px; font-size: .9rem; font-family: inherit;
}
.val-input:focus { outline: 2px solid var(--bs-red); border-color: var(--bs-red); }
.tpl-row td { background: inherit; }

/* color palette */
.palette-row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.palette-label { font-size: .85rem; color: var(--muted); }
.palette { display: flex; gap: 5px; flex-wrap: wrap; }
.swatch {
  width: 24px; height: 24px; border-radius: 5px; border: 2px solid var(--line);
  cursor: pointer; padding: 0; line-height: 1;
}
.swatch.none { background: #fff; color: var(--muted); font-size: .8rem; }
.swatch.sel { border-color: var(--ink); box-shadow: 0 0 0 2px rgba(0,0,0,.15); }
.swatch:hover { transform: scale(1.1); }
.palette-sm .swatch { width: 18px; height: 18px; border-width: 1px; }
.tpl-kind-sel { padding: 5px 6px; border: 1px solid var(--line); border-radius: 6px; font-size: .85rem; font-family: inherit; }

/* compare 2 months */
.compare-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.cmp-sum { font-size: .85rem; color: var(--muted); margin-bottom: 10px; }
.cmp-sum .neg { color: var(--bs-red); }
.cmp-sub { font-weight: 600; margin: 10px 0 4px; font-size: .9rem; }
.cmp-tbl td { padding: 4px 4px; }
.cmp-edit { border: 1px solid var(--line); border-radius: 5px; padding: 5px 6px; font-size: .85rem; font-family: inherit; }
.cmp-edit-name { width: 100%; }
.cmp-edit-amt { width: 90px; text-align: right; }
.cmp-actions { white-space: nowrap; text-align: right; }
.mini-btn {
  border: 1px solid var(--line); background: #fff; border-radius: 5px; cursor: pointer;
  width: 26px; height: 26px; font-size: .85rem; color: var(--ink); margin-left: 2px;
}
.mini-btn:hover { background: var(--bs-red); color: #fff; border-color: var(--bs-red); }
.cmp-add { display: flex; gap: 6px; margin: 6px 0 4px; }
.cmp-add input { padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; font-size: .85rem; }
.cmp-add input[type=number] { width: 90px; text-align: right; }
.cmp-add .btn-primary { padding: 6px 12px; }

@media (max-width: 720px) {
  .compare-grid { grid-template-columns: 1fr; }
}
