:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #f0f0ee;
  --border: #e3e3e0;
  --text: #191919;
  --text-2: #666666;
  --accent: #191919;
  --accent-text: #ffffff;
  --income-ink: #1b64be;   /* 글자용(대비 확보) */
  --expense-ink: #c2410c;
  --series-income: #257de0;  /* 차트용 */
  --series-expense: #f47230;
  --danger: #c62828;
  --grid: #ececec;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0f0f0f;
    --surface: #191919;
    --surface-2: #232323;
    --border: #2e2e2e;
    --text: #efefef;
    --text-2: #9c9c9c;
    --accent: #efefef;
    --accent-text: #191919;
    --income-ink: #6aabf1;
    --expense-ink: #f08a5a;
    --series-income: #338ced;
    --series-expense: #e76624;
    --danger: #ef6b6b;
    --grid: #2a2a2a;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", "Malgun Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
}
button, input, select { font: inherit; color: inherit; }
main { max-width: 640px; margin: 0 auto; padding: 0 16px; }
h2 { font-size: 15px; font-weight: 600; margin: 24px 0 10px; color: var(--text-2); }
.num { font-variant-numeric: tabular-nums; }

/* 상단 바 */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
}
.topbar h1 { font-size: 18px; margin: 0; min-width: 120px; text-align: center; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0;
  background: transparent; font-size: 26px; line-height: 1; cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

/* 요약 */
.summary {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 12px; margin-top: 4px;
}
.stat { display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 0; }
.stat-label { font-size: 13px; color: var(--text-2); }
.stat-value { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.stat-value.income { color: var(--income-ink); }
.stat-value.expense { color: var(--expense-ink); }

.budget {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; margin-top: 8px;
}
.budget-head { display: flex; justify-content: space-between; font-size: 13px; color: var(--text-2); margin-bottom: 8px; gap: 8px; }
.budget-head span:first-child { white-space: nowrap; }
.budget-head span:last-child { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; }
.budget-foot { font-size: 12px; color: var(--text-2); margin-top: 6px; font-variant-numeric: tabular-nums; }
.meter { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.meter-fill { height: 100%; width: 0; border-radius: 4px; background: var(--series-expense); transition: width .3s; }
.meter.over .meter-fill { background: var(--danger); }

/* 탭 */
.tabs { display: flex; gap: 4px; margin: 16px 0 4px; background: var(--surface-2); border-radius: 12px; padding: 4px; }
.tab { flex: 1; border: 0; background: transparent; padding: 8px; border-radius: 9px; cursor: pointer; color: var(--text-2); font-weight: 600; }
.tab.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

/* 내역 목록 */
.day { margin-top: 14px; }
.day-head { display: flex; justify-content: space-between; font-size: 13px; color: var(--text-2); padding: 0 4px 6px; }
.day-items { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.tx {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 12px 14px; border: 0; background: transparent; text-align: left; cursor: pointer;
}
.tx + .tx { border-top: 1px solid var(--border); }
.tx:hover { background: var(--surface-2); }
.tx-main { flex: 1; min-width: 0; }
.tx-cat { font-weight: 600; }
.tx-memo { font-size: 13px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-amt { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tx-amt.income { color: var(--income-ink); }
.tx-amt.expense { color: var(--text); }
.empty { text-align: center; color: var(--text-2); padding: 40px 0; }

/* 카테고리 막대 */
.cat-chart { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 6px 14px; }
.cat-row { display: grid; grid-template-columns: 84px 1fr auto; align-items: center; gap: 10px; padding: 8px 0; }
.cat-row + .cat-row { border-top: 1px solid var(--grid); }
.cat-name { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cat-track { height: 10px; }
.cat-bar { height: 100%; min-width: 2px; background: var(--series-expense); border-radius: 0 4px 4px 0; }
.cat-val { font-size: 13px; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.cat-val small { color: var(--text-2); margin-left: 4px; }

/* 6개월 추이 */
.legend { display: flex; gap: 14px; font-size: 13px; color: var(--text-2); margin-bottom: 8px; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.s-income { background: var(--series-income); }
.s-expense { background: var(--series-expense); }
.trend-chart { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 8px 4px; }
.trend-chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.trend-chart .gridline { stroke: var(--grid); stroke-width: 1; }
.trend-chart .axis-text { fill: var(--text-2); font-size: 11px; }
.trend-chart .bar-income { fill: var(--series-income); }
.trend-chart .bar-expense { fill: var(--series-expense); }
.trend-chart .hit { fill: transparent; cursor: default; }
.trend-chart .hit:hover, .trend-chart .hit.on { fill: var(--text); fill-opacity: .05; }
.trend-table { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 13px; font-variant-numeric: tabular-nums; }
.trend-table th, .trend-table td { padding: 6px 4px; text-align: right; border-bottom: 1px solid var(--grid); }
.trend-table th { color: var(--text-2); font-weight: 500; }
.trend-table th:first-child, .trend-table td:first-child { text-align: left; }

.tooltip {
  position: fixed; z-index: 20; pointer-events: none;
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font-size: 13px; box-shadow: 0 4px 16px rgba(0,0,0,.12);
  font-variant-numeric: tabular-nums;
}
.tooltip .tt-row { display: flex; align-items: center; gap: 6px; }

/* 설정 */
.row-form { display: flex; gap: 8px; }
.row-form input { flex: 1; min-width: 0; }
input, select {
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px;
  padding: 10px 12px; width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--series-income); outline-offset: -1px; }
.cat-list { list-style: none; margin: 10px 0; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.cat-list li { display: flex; align-items: center; gap: 8px; padding: 8px 8px 8px 14px; }
.cat-list li + li { border-top: 1px solid var(--border); }
.cat-list li span { flex: 1; }
.link-btn { border: 0; background: transparent; color: var(--text-2); padding: 6px 8px; border-radius: 8px; cursor: pointer; font-size: 13px; }
.link-btn:hover { background: var(--surface-2); color: var(--text); }
.link-btn.danger:hover { color: var(--danger); }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  border: 0; border-radius: 10px; padding: 10px 16px; cursor: pointer; font-weight: 600;
  background: var(--accent); color: var(--accent-text); text-decoration: none; white-space: nowrap;
}
.btn-secondary { background: var(--surface-2); color: var(--text); }
.btn-danger { background: transparent; color: var(--danger); padding-left: 4px; }
.btn:disabled { opacity: .5; }

.seg { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.seg-btn { flex: 1; border: 0; background: transparent; padding: 8px; border-radius: 9px; cursor: pointer; font-weight: 600; color: var(--text-2); }
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

/* 추가 버튼 */
.fab {
  position: fixed; right: max(20px, calc(50% - 300px)); bottom: calc(24px + env(safe-area-inset-bottom));
  width: 58px; height: 58px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--accent); color: var(--accent-text); font-size: 30px; line-height: 1;
  box-shadow: 0 6px 20px rgba(0,0,0,.2); z-index: 6;
}

/* 입력 창 */
dialog {
  border: 0; padding: 0; border-radius: 20px; width: min(440px, calc(100vw - 24px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
dialog::backdrop { background: rgba(0,0,0,.4); }
#txForm { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
#txForm h2 { margin: 0; color: var(--text); font-size: 18px; }
#txForm label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-2); }
#txForm label > input, #txForm label > select, .amount-wrap { color: var(--text); font-size: 16px; }
.amount-wrap { position: relative; }
.amount-wrap input { font-size: 22px; font-weight: 700; padding-right: 36px; text-align: right; font-variant-numeric: tabular-nums; }
.amount-wrap span { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--text-2); }
.dialog-actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
.spacer { flex: 1; }
.form-error { color: var(--danger); margin: 0; font-size: 13px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(96px + env(safe-area-inset-bottom));
  background: var(--accent); color: var(--accent-text); padding: 10px 16px; border-radius: 10px;
  font-size: 14px; z-index: 30; max-width: calc(100vw - 32px);
}

/* ===== v2: 통합 앱 ===== */
body { padding-bottom: calc(150px + env(safe-area-inset-bottom)); }
.month-nav { display: flex; align-items: center; gap: 12px; }
.page-title { font-size: 18px; margin: 0; padding: 8px 0; }
.muted { color: var(--text-2); font-weight: 400; }
.small { font-size: 12px; }
h2 small { font-size: 12px; margin-left: 4px; }
h3 { font-size: 14px; margin: 18px 0 8px; color: var(--text-2); font-weight: 600; }

/* 하단 탭 */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 7;
  display: flex; justify-content: center;
  background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}
.nav-btn {
  flex: 1; max-width: 120px; border: 0; background: transparent; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 11px; color: var(--text-2); padding: 4px 0; border-radius: 10px;
}
.nav-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav-btn.active { color: var(--text); font-weight: 700; }
.nav-btn.active svg { stroke-width: 2.2; }
.fab { bottom: calc(80px + env(safe-area-inset-bottom)); }
.toast { bottom: calc(150px + env(safe-area-inset-bottom)); }

/* 공통 카드형 목록 */
.list-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.section-meta { font-size: 12px; color: var(--text-2); text-align: right; font-variant-numeric: tabular-nums; }
.empty.small { padding: 18px 12px; font-size: 13px; background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }

/* 납입 일정 */
.sched-row { display: flex; align-items: center; gap: 10px; padding: 10px 12px; }
.sched-row + .sched-row { border-top: 1px solid var(--border); }
.sched-row.done { opacity: .6; }
.sched-day { width: 40px; flex-shrink: 0; text-align: center; font-weight: 700; font-size: 13px; background: var(--surface-2); border-radius: 8px; padding: 6px 0; font-variant-numeric: tabular-nums; }
.sched-day.overdue { color: var(--danger); }
.sched-main { flex: 1; min-width: 0; border: 0; background: transparent; text-align: left; padding: 0; cursor: pointer; }
.sched-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sched-sub { font-size: 12px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sched-amt { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; font-size: 14px; }
.sched-done { font-size: 12px; color: var(--text-2); white-space: nowrap; width: 58px; text-align: center; }
.btn-small { padding: 6px 12px; font-size: 13px; border-radius: 8px; }
.sched-row .btn-small { width: 58px; }

/* 재무 현황 */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; min-width: 0; }
.tile-label { font-size: 12px; color: var(--text-2); }
.tile-value { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-sub { font-size: 11px; color: var(--text-2); margin-top: 2px; }
.kv-card { margin-top: 8px; padding: 4px 14px; }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; font-size: 14px; }
.kv + .kv { border-top: 1px solid var(--grid); }
.kv > span:first-child { color: var(--text-2); flex-shrink: 0; }
.kv > span:last-child { text-align: right; min-width: 0; overflow-wrap: anywhere; }
.kv.strong > span { color: var(--text); font-weight: 700; }
.kv .pre { white-space: pre-line; text-align: left; }
.kv-list { background: var(--surface-2); border-radius: 12px; padding: 2px 12px; }
.kv-list .kv + .kv { border-top-color: var(--border); }

/* 만기 */
.mat-row { display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 14px; border: 0; background: transparent; text-align: left; cursor: pointer; }
.mat-row + .mat-row { border-top: 1px solid var(--border); }
.mat-date { text-align: right; font-size: 13px; font-variant-numeric: tabular-nums; }
.mat-date small { color: var(--text-2); }

/* 금융 탭 */
.chips-row { display: flex; gap: 6px; overflow-x: auto; padding: 12px 0 4px; scrollbar-width: none; }
.chips-row::-webkit-scrollbar { display: none; }
.chip-btn { flex-shrink: 0; border: 1px solid var(--border); background: var(--surface); padding: 7px 14px; border-radius: 999px; cursor: pointer; font-weight: 600; color: var(--text-2); }
.chip-btn.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.fin-summary { font-size: 13px; color: var(--text-2); margin: 8px 2px 10px; font-variant-numeric: tabular-nums; }
.fin-summary:empty { display: none; }
.item-card {
  display: block; width: 100%; text-align: left; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; margin-bottom: 8px;
}
.item-card:hover { border-color: color-mix(in srgb, var(--text) 25%, var(--border)); }
.item-card.inactive { opacity: .55; }
.item-head { display: flex; gap: 12px; align-items: flex-start; }
.item-title { flex: 1; min-width: 0; }
.item-name { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.item-sub { font-size: 12px; color: var(--text-2); margin-top: 2px; }
.item-right { text-align: right; flex-shrink: 0; }
.item-amt { font-weight: 700; font-variant-numeric: tabular-nums; }
.item-line { font-size: 12px; color: var(--text-2); margin-top: 6px; font-variant-numeric: tabular-nums; }
.item-line:empty { display: none; }
.item-card .meter { margin-top: 10px; height: 6px; }
.meter.good .meter-fill { background: var(--series-income); }
.badge { font-size: 11px; font-weight: 600; color: var(--text-2); background: var(--surface-2); padding: 2px 6px; border-radius: 6px; }
.chips-wrap { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }
.chip { font-size: 12px; background: var(--surface-2); padding: 3px 8px; border-radius: 6px; font-variant-numeric: tabular-nums; }
.chip.muted { color: var(--text-2); }
.ok { color: var(--income-ink); font-weight: 600; }
.warn { color: var(--expense-ink); font-weight: 600; }
.benefits { margin: 8px 0 0; padding-left: 18px; font-size: 13px; }
.benefits li { margin: 2px 0; }
.benefits li.muted { list-style: none; margin-left: -18px; }

/* 시트 (금융 정보 상세/입력) */
.sheet { width: min(520px, calc(100vw - 16px)); max-height: calc(100dvh - 32px); overflow-y: auto; }
.sheet-inner { padding: 20px 20px 0; display: flex; flex-direction: column; gap: 10px; }
.sheet-kind { font-size: 12px; color: var(--text-2); font-weight: 600; }
.sheet-title { margin: 0 0 6px; color: var(--text); font-size: 20px; }
.sheet-inner h3 { margin: 12px 0 0; }
.sheet-title + h3 { margin-top: 0; }
.sheet-inner .dialog-actions { position: sticky; bottom: 0; background: var(--surface); padding: 12px 0 18px; margin-top: 8px; border-top: 1px solid var(--border); }
.form-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field:has(textarea), .field-wide, .field:has(.switch) { grid-column: 1 / -1; }
.field-label { font-size: 13px; color: var(--text-2); }
.req { color: var(--danger); }
.field-help { font-size: 11px; color: var(--text-2); }
textarea { border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 10px 12px; width: 100%; font: inherit; color: inherit; resize: vertical; }
textarea:focus { outline: 2px solid var(--series-income); outline-offset: -1px; }
.suffix-wrap { position: relative; }
.suffix-wrap input { padding-right: 32px; text-align: right; font-variant-numeric: tabular-nums; }
.suffix-wrap span { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--text-2); font-size: 14px; }
.switch { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); }
.cov-editor { display: flex; flex-direction: column; gap: 8px; }
.cov-list { display: flex; flex-direction: column; gap: 6px; }
.cov-row { display: grid; grid-template-columns: 1fr 120px auto; gap: 6px; align-items: center; }
.cov-row input:nth-child(2) { text-align: right; font-variant-numeric: tabular-nums; }
.cov-editor > .btn { align-self: flex-start; }
@media (max-width: 380px) {
  .form-fields { grid-template-columns: 1fr; }
  .cov-row { grid-template-columns: 1fr 100px auto; }
}
