* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

:root {
  --bg: #F2F4F6;
  --surface: #FFFFFF;
  --text-primary: #191F28;
  --text-secondary: #8B95A1;
  --text-tertiary: #B0B8C1;
  --line: #F2F4F6;
  --line-strong: #E5E8EB;
  --blue: #3182F6;
  --blue-bg: #F2F8FF;
  --blue-dark: #1B64DA;
  --red: #F04452;
  --red-bg: #FFF0F1;
  --red-light: #F5828A;
  --green: #00C285;
  --black: #191F28;
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --font-sans: 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', monospace;
}

body {
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}

#app { min-height: 100vh; display: flex; flex-direction: column; }

button {
  font-family: var(--font-sans);
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

input, select, textarea {
  font-family: var(--font-sans);
}

/* ===== Topbar ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg);
  padding: max(16px, env(safe-area-inset-top)) 20px 8px;
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 4px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 17px;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}
.brand .stamp {
  width: 28px; height: 28px;
  border-radius: 9px;
  background: var(--black);
  display: flex; align-items: center; justify-content: center;
  color: #fff;
  flex-shrink: 0;
}

.month-nav {
  display: flex;
  align-items: center;
  gap: 8px;
}
.month-nav button {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--surface);
  color: var(--text-secondary);
  display: flex; align-items: center; justify-content: center;
  transition: background .15s;
}
.month-nav button:active { background: var(--line-strong); }
.month-label {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  min-width: 76px;
  text-align: center;
}

.icon-btn {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--surface);
  color: var(--text-secondary);
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:active { background: var(--line-strong); }
.icon-btn.spinning svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 4px 0 2px;
}
.tabs::-webkit-scrollbar { display: none; }
.tab-btn {
  flex-shrink: 0;
  font-size: 14px;
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-secondary);
  white-space: nowrap;
}
.tab-btn.active {
  background: var(--black);
  color: #fff;
}

.content {
  flex: 1;
  padding: 4px 20px 100px;
}

/* ===== Balance hero card ===== */
.balance-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 22px 20px;
  margin: 12px 0 16px;
}
.balance-card .label {
  font-size: 13px;
  color: var(--text-secondary);
  margin: 0 0 4px;
}
.balance-card .value {
  font-family: var(--font-mono);
  font-size: 32px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.02em;
  margin: 0 0 14px;
}
.balance-card .value .unit {
  font-size: 15px;
  color: var(--text-secondary);
  font-weight: 500;
  font-family: var(--font-sans);
}
.balance-card .sub-row {
  display: flex;
  gap: 16px;
}
.balance-card .sub-row span {
  font-size: 13px;
  font-family: var(--font-mono);
}
.balance-card .sub-income { color: var(--blue); }
.balance-card .sub-expense { color: var(--red); }

.progress-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--surface);
  padding: 6px 12px;
  border-radius: 999px;
  margin-bottom: 16px;
}

/* ===== Section title ===== */
.section-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin: 20px 2px 10px;
}
.section-title .count {
  margin-left: auto;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-tertiary);
}

/* ===== Group list (대시보드 그룹별 카드) ===== */
.group-list {
  display: flex;
  flex-direction: column;
}
.group-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 2px;
  border-bottom: 1px solid var(--line);
}
.group-row:last-child { border-bottom: none; }
.group-row-left {
  display: flex;
  align-items: center;
  gap: 10px;
}
.group-icon {
  width: 36px; height: 36px;
  border-radius: 11px;
  background: var(--line);
  display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary);
  flex-shrink: 0;
}
.group-icon.accent { background: var(--blue-bg); color: var(--blue-dark); }
.group-name { font-size: 15px; color: var(--text-primary); }
.group-name.muted { color: var(--text-secondary); }
.group-amount {
  font-family: var(--font-mono);
  font-size: 15px;
  font-weight: 500;
}
.group-amount.expense { color: var(--red); }
.group-amount.income { color: var(--blue); }
.group-amount.muted { color: var(--red-light); }

/* ===== Alert / reminder card ===== */
.alert-card {
  background: var(--blue-bg);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  margin-top: 4px;
}
.alert-card.urgent { background: var(--red-bg); }
.alert-card-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 4px;
}
.alert-card-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--blue-dark);
  display: flex;
  align-items: center;
  gap: 6px;
}
.alert-card.urgent .alert-card-title { color: var(--red); }
.alert-card-dday {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--blue-dark);
}
.alert-card.urgent .alert-card-dday { color: var(--red); }
.alert-card-body {
  font-size: 14px;
  color: var(--text-primary);
  margin: 0;
}

/* ===== Loading / empty ===== */
.loading-screen, .empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 60px 20px;
  color: var(--text-tertiary);
  font-size: 14px;
}
.loading-screen svg { animation: spin 0.8s linear infinite; }

/* ===== FAB ===== */
.fab {
  position: fixed;
  right: 20px;
  bottom: max(24px, env(safe-area-inset-bottom));
  width: 56px; height: 56px;
  border-radius: 50%;
  background: var(--blue);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 16px rgba(49,130,246,0.35);
  z-index: 30;
}
.fab:active { background: var(--blue-dark); }

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--black);
  color: #fff;
  font-size: 13px;
  padding: 10px 18px;
  border-radius: 999px;
  z-index: 50;
  animation: toast-in 0.2s ease;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ===== Modal/sheet ===== */
.sheet-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15,20,27,0.45);
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet {
  background: var(--bg);
  border-radius: 24px 24px 0 0;
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 14px 20px max(24px, env(safe-area-inset-bottom));
  animation: sheet-up .22s ease-out;
}
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-handle {
  width: 36px; height: 4px;
  background: var(--line-strong);
  border-radius: 4px;
  margin: 0 auto 16px;
}
.sheet h3 {
  margin: 0 0 18px;
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
}

.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 7px;
}
.field input, .field select, .field textarea {
  width: 100%;
  padding: 12px 14px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 15px;
  color: var(--text-primary);
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: 2px solid var(--blue);
  outline-offset: -1px;
}
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; min-width: 0; }

.seg {
  display: flex;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface);
}
.seg button {
  flex: 1;
  padding: 11px 0;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
}
.seg button.active { background: var(--black); color: #fff; }

/* 동적 그룹 선택 칩 (가계/운송/세탁/.../+ 추가) */
.group-chip-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.group-chip {
  font-size: 14px;
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-secondary);
  font-weight: 500;
}
.group-chip.active { background: var(--black); color: #fff; }
.group-chip.add-new {
  background: transparent;
  border: 1.5px dashed var(--line-strong);
  color: var(--text-tertiary);
}

.amount-display {
  font-family: var(--font-mono);
  font-size: 34px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.02em;
  margin: 4px 0 18px;
}
.amount-display.expense { color: var(--red); }
.amount-display .unit { font-size: 16px; color: var(--text-secondary); font-weight: 500; font-family: var(--font-sans); }

.sheet-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}
.btn {
  flex: 1;
  padding: 14px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
}
.btn.primary {
  background: var(--blue);
  color: #fff;
}
.btn.danger-text { color: var(--red); flex: none; padding: 14px 14px; background: transparent; }

.settings-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 2px;
  border-bottom: 1px solid var(--line);
}
.settings-row:last-child { border-bottom: none; }
.settings-row .label-text { font-size: 15px; font-weight: 500; color: var(--text-primary); }
.settings-row .label-sub { font-size: 12px; color: var(--text-tertiary); margin-top: 2px; }

/* ===== Ledger (entries/체크리스트 공용 리스트) ===== */
.ledger {
  background: var(--surface);
  border-radius: var(--radius-md);
  overflow: hidden;
}
.ledger-group-label {
  padding: 10px 16px;
  background: var(--bg);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-tertiary);
}
.entry-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 16px;
  border-bottom: 1px solid var(--line);
  position: relative;
}
.entry-row:last-child { border-bottom: none; }
.entry-row.done { opacity: 0.55; }

.check {
  width: 24px; height: 24px;
  border-radius: 50%;
  border: 1.5px solid var(--line-strong);
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface);
  transition: all .15s;
}
.check.checked {
  background: var(--blue);
  border-color: var(--blue);
  color: white;
}

.entry-main { flex: 1; min-width: 0; }
.entry-name {
  font-size: 15px;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.entry-row.done .entry-name { text-decoration: line-through; }
.entry-meta {
  font-size: 12px;
  color: var(--text-tertiary);
  margin-top: 2px;
  display: flex;
  gap: 6px;
  align-items: center;
}
.badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--text-secondary);
  font-weight: 500;
}

.entry-amount {
  font-family: var(--font-mono);
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  flex-shrink: 0;
}
.entry-amount.income { color: var(--blue); }
.entry-amount.expense { color: var(--red); }
.entry-row.done .entry-amount { color: var(--text-tertiary); }

.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-tertiary);
}
.empty-state svg { margin-bottom: 10px; display: block; margin-left: auto; margin-right: auto; }
.empty-state p { margin: 0; font-size: 14px; }

/* ===== Installments ===== */
.inst-row {
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.inst-row:last-child { border-bottom: none; }
.inst-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 8px;
}
.inst-name { font-size: 15px; font-weight: 500; color: var(--text-primary); display: flex; align-items: center; gap: 6px; }
.inst-frac { font-family: var(--font-mono); font-size: 12px; color: var(--text-secondary); }
.inst-bar {
  height: 6px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 8px;
}
.inst-bar-fill { height: 100%; background: var(--blue); border-radius: 4px; }
.inst-bar-fill.finishing { background: var(--red); }
.inst-bottom {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-tertiary);
}
.inst-bottom .amt { font-family: var(--font-mono); }

/* ===== Calendar ===== */
.cal-legend {
  display: flex;
  gap: 14px;
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.cal-legend span { display: flex; align-items: center; gap: 5px; }
.cal-dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.cal-dot.expense-done { background: var(--red); }
.cal-dot.expense-upcoming { background: var(--red-light); }
.cal-dot.income { background: var(--blue); }

.cal-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  margin-bottom: 4px;
}
.cal-weekdays span {
  text-align: center;
  font-size: 11px;
  color: var(--text-tertiary);
  font-weight: 500;
}

.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  margin-bottom: 18px;
}
.cal-cell {
  border-radius: var(--radius-sm);
  background: var(--surface);
  min-height: 54px;
  padding: 6px 4px;
  text-align: left;
  font-family: inherit;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.cal-cell.empty { visibility: hidden; pointer-events: none; }
.cal-cell.today { outline: 1.5px solid var(--blue); outline-offset: -1.5px; }
.cal-cell.selected { background: var(--black); }
.cal-cell.selected .cal-day-num { color: #fff; }
.cal-day-num { font-size: 12px; color: var(--text-secondary); font-weight: 500; }
.cal-cell.today .cal-day-num { color: var(--blue); font-weight: 600; }
.cal-dots { display: flex; gap: 2px; flex-wrap: wrap; }

.day-list-header {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 8px;
}

/* ===== Stats ===== */
.stats-toggle {
  display: flex;
  gap: 6px;
  margin-bottom: 14px;
}
.stats-toggle button {
  flex: 1;
  padding: 9px 0;
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}
.stats-toggle button.active {
  background: var(--black);
  color: #fff;
}
.stat-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 16px;
  border-bottom: 1px solid var(--line);
}
.stat-row:last-child { border-bottom: none; }
.stat-name { font-size: 15px; font-weight: 500; flex: 1; }
.stat-sub { font-size: 12px; color: var(--text-tertiary); margin-top: 2px; }
.stat-amounts { text-align: right; }
.stat-expense { font-family: var(--font-mono); font-size: 15px; font-weight: 600; color: var(--red); }
.stat-income { font-family: var(--font-mono); font-size: 12px; color: var(--blue); margin-top: 2px; }
.stat-bar-bg {
  height: 5px;
  background: var(--bg);
  border-radius: 3px;
  margin-top: 6px;
  overflow: hidden;
}
.stat-bar-fill { height: 100%; background: var(--red); border-radius: 3px; }
