:root {
  --bg: #f4f6f3;
  --panel: #ffffff;
  --fg: #182019;
  --muted: #6a756c;
  --line: #dde3dd;
  --accent: #1f4d3a;
  --accent-soft: #e2efe8;
  --ok: #1d7a4b;
  --bad: #b8422f;
  --info: #2f6db5;
  --sum-bg: #fbf3df;
  --radius: 12px;
  --sans: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", "Malgun Gothic", system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121714;
    --panel: #1b221d;
    --fg: #e8ece8;
    --muted: #94a098;
    --line: #2b352e;
    --accent: #7fc8a4;
    --accent-soft: #1f3328;
    --ok: #6fd39a;
    --bad: #ef8a76;
    --info: #7fb0ea;
    --sum-bg: #2a2a1f;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.45;
  font-variant-numeric: tabular-nums;
  padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px));
}
h1 { font-size: 20px; margin: 0; font-weight: 700; }
h2 { font-size: 15px; margin: 0 0 6px; font-weight: 600; }
h3 { font-size: 16px; margin: 0 0 8px; }
p { margin: 0; }
.muted { color: var(--muted); font-size: 13px; }
.note { color: var(--muted); font-size: 12.5px; margin-bottom: 10px; }
.empty { padding: 20px 4px; }

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) 10px max(16px, env(safe-area-inset-left, 0px));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.icon-btn {
  border: 1px solid var(--line); background: var(--panel); color: var(--fg);
  width: 36px; height: 36px; border-radius: 50%; font-size: 18px; cursor: pointer;
}

main {
  padding: 14px max(16px, env(safe-area-inset-right, 0px)) 16px max(16px, env(safe-area-inset-left, 0px));
  display: grid; gap: 16px; max-width: 1100px; margin: 0 auto;
}
.tab { display: grid; gap: 16px; }
.tab[hidden] { display: none; }

.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
@media (min-width: 720px) { .tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.tile, .panel, .card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; min-width: 0;
}
.tile { display: grid; gap: 2px; }
.label { font-size: 12px; color: var(--muted); }
.num { font-size: 20px; font-weight: 700; letter-spacing: -0.01em; overflow-wrap: anywhere; }
.sub { font-size: 11.5px; color: var(--muted); }
.up { color: var(--ok); }
.down { color: var(--bad); }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 230px), 1fr)); gap: 10px; }
.card { display: grid; gap: 3px; border-top: 3px solid var(--acc, var(--accent)); }
.card .bank { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.card .name { font-weight: 600; }
.card .bal { font-size: 19px; font-weight: 700; }
.card svg { width: 100%; height: 40px; display: block; margin-top: 6px; }
.chip { font-size: 11px; padding: 0 7px; border-radius: 999px; border: 1px solid var(--line); background: var(--bg); white-space: nowrap; }

.panel { display: grid; gap: 6px; align-content: start; }
.chart { position: relative; width: 100%; min-height: 240px; }
.chart canvas { width: 100% !important; }

.cats { display: grid; gap: 8px; }
.cat { display: grid; grid-template-columns: 4.5rem 1fr 6.5rem; gap: 8px; align-items: center; font-size: 14px; }
.cat .bar { height: 12px; border-radius: 6px; background: var(--accent); opacity: .85; }
.cat .bar.muted-bar { background: var(--line); opacity: 1; }
.cat .v { text-align: right; }
.cat.sum { border-top: 1px solid var(--line); padding-top: 6px; color: var(--muted); }

.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.filters .input { grid-column: 1 / -1; }
@media (min-width: 720px) { .filters { grid-template-columns: repeat(4, 1fr); } .filters .input { grid-column: auto; } }

.select, .input, .btn {
  font: inherit; color: var(--fg); background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; min-height: 40px; min-width: 0;
}
.input.narrow { width: 5.5rem; flex: 0 0 auto; }
.btn { cursor: pointer; text-decoration: none; text-align: center; display: inline-block; }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
@media (prefers-color-scheme: dark) { .btn.primary { color: #0f1a14; } }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: 13px; white-space: nowrap; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > .select, .row > .input { flex: 1 1 140px; }
.form { display: grid; gap: 8px; }
.sub-form { padding: 10px; border: 1px dashed var(--line); border-radius: 10px; }
label { font-size: 13px; color: var(--muted); }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; color: var(--fg); }
input[type="file"] { font-size: 14px; }

.txlist, .plain { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.tx {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; align-items: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; cursor: pointer;
}
.tx .d { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx .m { font-size: 12px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.tx .a { font-weight: 600; text-align: right; grid-column: 2; grid-row: 1 / span 2; white-space: nowrap; }
.tx .d, .tx .m { grid-column: 1; min-width: 0; }
.tx .a.up { color: var(--ok); }
.tx .cat-chip { font-size: 11px; padding: 0 6px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.tx .cat-chip.manual { outline: 1px solid var(--accent); }
.date-head { font-size: 12px; color: var(--muted); padding: 8px 4px 2px; }

.plain li { display: flex; justify-content: space-between; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.plain li:last-child { border-bottom: 0; }
.plain .meta { font-size: 12px; color: var(--muted); }
.rules li { padding: 6px 0; }

.result { border-radius: 10px; padding: 12px; background: var(--accent-soft); font-size: 14px; display: grid; gap: 4px; }
.result.err { background: color-mix(in srgb, var(--bad) 14%, var(--panel)); }

.bottom {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  background: var(--panel); border-top: 1px solid var(--line);
  padding: 6px max(8px, env(safe-area-inset-right, 0px)) calc(6px + env(safe-area-inset-bottom, 0px)) max(8px, env(safe-area-inset-left, 0px));
}
.bottom button { position: relative; background: none; border: 0; color: var(--muted); font: inherit; font-size: 12.5px; padding: 8px 2px; border-radius: 8px; cursor: pointer; white-space: nowrap; }
.bottom button.active { color: var(--accent); font-weight: 700; background: var(--accent-soft); }
.nav-badge { position: absolute; top: 2px; right: 4px; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 8px; background: var(--bad); color: #fff; font-size: 10px; font-weight: 700; line-height: 16px; text-align: center; }
#lg-map-link a { color: var(--accent); font-weight: 600; }

dialog { border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--fg); padding: 16px; width: min(92vw, 380px); }
dialog::backdrop { background: rgba(0,0,0,.4); }

.toast {
  position: fixed; left: 50%; bottom: calc(76px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%);
  background: var(--fg); color: var(--bg); padding: 10px 14px; border-radius: 10px; font-size: 14px; z-index: 20; max-width: 90vw;
}

/* 계좌별 작은 차트 */
.minis { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 250px), 1fr)); gap: 10px; }
.mini { display: grid; gap: 2px; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; border-top: 3px solid var(--acc, var(--accent)); min-width: 0; }
.mini .head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.mini .name { font-weight: 600; font-size: 13.5px; }
.mini .val { font-weight: 700; font-size: 14px; white-space: nowrap; }
.mini .chart { min-height: 120px; height: 120px; }

/* 가계부 표 */
.ledger-controls { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.ledger-controls .select:last-child { grid-column: 1 / -1; }
@media (min-width: 720px) { .ledger-controls { grid-template-columns: repeat(3, 1fr); } .ledger-controls .select:last-child { grid-column: auto; } }
.ledger td.k0, .ledger th.k0 { position: sticky; left: 0; z-index: 2; text-align: left; min-width: 6.5rem; background: var(--sum-bg); font-weight: 500; }
.ledger thead th.k0 { z-index: 4; background: var(--bg); text-align: center; }
.ledger tr.sum td.k0 { text-align: center; }
.ledger tr.saving td.k0 { background: color-mix(in srgb, var(--info) 12%, var(--panel)); }
.ledger tr.saving-sum td { background: color-mix(in srgb, var(--info) 18%, var(--panel)); font-weight: 700; }
.ledger tr.grand td { background: var(--accent-soft); font-weight: 700; }
.row.between { justify-content: space-between; align-items: center; }
.ledger-wrap { overflow: auto; max-width: 100%; border: 1px solid var(--line); border-radius: 10px; -webkit-overflow-scrolling: touch; }
.ledger { border-collapse: separate; border-spacing: 0; font-size: 12.5px; min-width: 100%; }
.ledger th, .ledger td { padding: 6px 8px; border-bottom: 1px solid var(--line); border-right: 1px solid var(--line); white-space: nowrap; text-align: right; background: var(--panel); }
.ledger thead th { position: sticky; top: 0; z-index: 3; background: var(--bg); text-align: center; font-weight: 600; }
.ledger th.g, .ledger td.g { position: sticky; left: 0; z-index: 2; text-align: center; font-weight: 600; min-width: 5.5rem; background: var(--sum-bg); vertical-align: middle; }
.ledger th.k, .ledger td.k { position: sticky; left: 5.5rem; z-index: 2; text-align: center; min-width: 4.5rem; background: var(--sum-bg); }
.ledger thead th.g, .ledger thead th.k { z-index: 4; background: var(--bg); }
.ledger td.v { cursor: pointer; }
.ledger td.v:hover { background: var(--accent-soft); }
.ledger td.zero { color: var(--muted); }
.ledger tr.c-claim td.v, .ledger tr.c-claim td.k { color: var(--bad); font-weight: 600; }
.ledger tr.c-excl td.v, .ledger tr.c-excl td.k { color: var(--info); }
.ledger tr.sum td { background: var(--sum-bg); font-weight: 700; }
.ledger tr.sum td.k { font-weight: 700; }
.ledger tr.group-start td, .ledger tr.group-start th { border-top: 2px solid var(--muted); }
.ledger td.tot, .ledger th.tot { font-weight: 700; }
.ledger tr.all td.g { background: var(--accent-soft); }
.ledger td.sel { outline: 2px solid var(--accent); outline-offset: -2px; }

.entries li { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 10px; cursor: pointer; align-items: center; }
.entries .d { grid-column: 1; min-width: 0; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entries .m { grid-column: 1; min-width: 0; font-size: 12px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; }
.entries .a { font-weight: 600; grid-column: 2; grid-row: 1 / span 2; white-space: nowrap; }
.entries .claim { font-size: 11px; padding: 0 6px; border-radius: 999px; background: color-mix(in srgb, var(--bad) 14%, var(--panel)); color: var(--bad); }
.entries .chip { font-size: 11px; }
/* 매핑 대기 목록 */
.maplist { min-width: 0; }
.maplist li { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; padding: 10px 0; min-width: 0; }
.maprow .mf .input { width: 100%; min-width: 0; }
.maprow .mh { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.maprow .mh .d { font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.maprow .mh .a { font-weight: 700; white-space: nowrap; }
.maprow .m { font-size: 12px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; }
.maprow .sug { font-size: 11px; padding: 0 6px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.maprow .mf { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.maprow .mf .input { min-height: 36px; padding: 6px 8px; font-size: 14px; }
.maprow .actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.maprow .actions .spacer { flex: 1; }

.maprow .mf .select { min-height: 36px; padding: 6px 8px; font-size: 14px; width: 100%; min-width: 0; }

/* 항목 코드 관리 */
.codes { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr)); gap: 14px; }
.code-group { display: grid; gap: 6px; align-content: start; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.code-group h3 { font-size: 14px; margin: 0; }
.code-group .plain li { padding: 5px 0; font-size: 13.5px; }
.code-group .code { display: inline-block; min-width: 2.2rem; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.code-group .used { color: var(--muted); font-size: 11.5px; margin-left: 6px; }
.code-add .input.narrow { width: 4.2rem; }
.code-add .input:not(.narrow) { flex: 1 1 120px; }
.field .select { width: 100%; }
.kind-toggle { gap: 16px; }
.kind-toggle .check { font-size: 14px; }
.kind-chip { font-size: 11px; padding: 0 6px; border-radius: 999px; background: color-mix(in srgb, var(--ok) 16%, var(--panel)); color: var(--ok); }
.entries .a.income, .maprow .a.income { color: var(--ok); }
.ledger tr.income-sum td { background: color-mix(in srgb, var(--ok) 16%, var(--panel)); font-weight: 700; }

.field { display: grid; gap: 3px; flex: 1 1 120px; min-width: 0; }
.field span { font-size: 12px; color: var(--muted); }
.field .input { width: 100%; }
#entry-dialog { width: min(94vw, 460px); }

.login-body { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login { display: grid; gap: 14px; justify-items: center; width: min(92vw, 360px); background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px 22px; }
.login-form { display: grid; gap: 8px; width: 100%; }
.login .error { color: var(--bad); font-size: 13px; }
