:root {
  --bg: #f4f6f5; --card: #ffffff; --line: #e3e7e5; --text: #1d2321; --muted: #7a8582;
  --pri: #0f7b6c; --pri-soft: #e1f2ee; --inc: #2563eb; --exp: #e5484d; --tr: #6b7280;
  --warn: #c2410c; --warn-soft: #fff1e6; --sat: #2563eb; --sun: #e5484d;
  --shadow: 0 2px 10px rgba(0,0,0,.12);
  --nav-h: 58px; --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111615; --card: #1a2120; --line: #2a3331; --text: #e6ebe9; --muted: #8d9895;
    --pri: #2bb39e; --pri-soft: #183a34; --inc: #6ea0ff; --exp: #ff7a7e; --tr: #9aa3ad;
    --warn: #ff9a57; --warn-soft: #3a2616; --sat: #6ea0ff; --sun: #ff7a7e; --shadow: 0 2px 12px rgba(0,0,0,.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111615; --card: #1a2120; --line: #2a3331; --text: #e6ebe9; --muted: #8d9895;
  --pri: #2bb39e; --pri-soft: #183a34; --inc: #6ea0ff; --exp: #ff7a7e; --tr: #9aa3ad;
  --warn: #ff9a57; --warn-soft: #3a2616; --sat: #6ea0ff; --sun: #ff7a7e; --shadow: 0 2px 12px rgba(0,0,0,.5);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 15px/1.4 system-ui, -apple-system, "Malgun Gothic", "Apple SD Gothic Neo", sans-serif; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
#app { max-width: 640px; margin: 0 auto; min-height: 100%; background: var(--bg); position: relative; }
@media (min-width: 700px) { #app { border-left: 1px solid var(--line); border-right: 1px solid var(--line); } }

/* 상단 */
#top { position: sticky; top: 0; z-index: 5; background: var(--card); border-bottom: 1px solid var(--line); padding-top: env(safe-area-inset-top, 0px); }
.bar { display: flex; align-items: center; gap: 4px; height: 50px; padding: 0 8px; }
.bar .ttl { font-size: 18px; font-weight: 700; padding: 6px 4px; }
.bar .sp { flex: 1; }
.ic { width: 40px; height: 40px; border-radius: 20px; display: inline-flex; align-items: center; justify-content: center; font-size: 22px; }
.ic:active { background: var(--line); }
.ic svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.bar input.q { flex: 1; height: 38px; border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; background: var(--bg); }
.tabs { display: flex; padding: 0 4px; }
.tabs button { flex: 1; padding: 10px 0 9px; color: var(--muted); border-bottom: 3px solid transparent; font-weight: 600; }
.tabs button.on { color: var(--pri); border-bottom-color: var(--pri); }
.sum3 { display: flex; border-top: 1px solid var(--line); }
.sum3 div { flex: 1; text-align: center; padding: 7px 0; }
.sum3 small { display: block; color: var(--muted); font-size: 12px; }
.sum3 b { font-size: 15px; }

/* 본문 */
#view { padding-bottom: calc(var(--nav-h) + var(--safe-b) + 90px); min-height: 60vh; }
.inc { color: var(--inc); } .exp { color: var(--exp); } .tr { color: var(--tr); }
.muted { color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.card { background: var(--card); margin: 10px 10px 0; border-radius: 14px; border: 1px solid var(--line); overflow: hidden; }
.card h3 { margin: 0; padding: 12px 14px 8px; font-size: 15px; }
.card .pad { padding: 0 14px 12px; }

.dh { display: flex; align-items: baseline; gap: 6px; padding: 10px 14px 6px; border-bottom: 1px solid var(--line); }
.dh b { font-size: 20px; }
.dh .wd { font-size: 12px; padding: 1px 6px; border-radius: 6px; background: var(--bg); }
.wd0 { color: var(--sun); } .wd6 { color: var(--sat); }
.dh .ymt { color: var(--muted); font-size: 12px; }
.dh .sp { flex: 1; }
.dh .inc, .dh .exp { font-size: 13px; font-weight: 600; }

.row { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); min-height: 56px; }
.row:last-child { border-bottom: 0; }
.row:active { background: var(--bg); }
.rc { width: 70px; flex: none; font-size: 13px; color: var(--muted); line-height: 1.25; word-break: keep-all; }
.rc small { display: block; font-size: 11px; }
.rm { flex: 1; min-width: 0; }
.rt { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rs { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ra { font-weight: 700; white-space: nowrap; }
.ra.income { color: var(--inc); } .ra.expense { color: var(--exp); } .ra.transfer { color: var(--tr); }
.row.unc { background: var(--warn-soft); }
.row.unc .rc { color: var(--warn); font-weight: 700; }

/* 달력 */
.cal { display: grid; grid-template-columns: repeat(7, 1fr); background: var(--card); border-bottom: 1px solid var(--line); }
.cal .h { text-align: center; font-size: 12px; padding: 6px 0; color: var(--muted); border-bottom: 1px solid var(--line); }
.cal .c { min-height: 64px; padding: 3px 3px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); overflow: hidden; font-size: 10.5px; line-height: 1.3; text-align: right; }
.cal .c:nth-child(7n) { border-right: 0; }
.cal .c .d { font-size: 12px; text-align: left; font-weight: 600; }
.cal .c.today .d { color: var(--pri); }
.cal .c.sel { background: var(--pri-soft); }
.cal .c div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* 표 */
table.t { width: 100%; border-collapse: collapse; background: var(--card); }
table.t th, table.t td { padding: 11px 10px; border-bottom: 1px solid var(--line); text-align: right; font-size: 14px; }
table.t th { color: var(--muted); font-weight: 600; font-size: 12px; }
table.t td:first-child, table.t th:first-child { text-align: left; }
table.t tr.cur td { background: var(--pri-soft); }
table.t tbody tr:active td { background: var(--bg); }

.bars .b { display: flex; align-items: center; gap: 8px; padding: 6px 14px; }
.bars .b span:first-child { width: 90px; flex: none; font-size: 14px; }
.bars .b .g { flex: 1; height: 8px; border-radius: 4px; background: var(--bg); overflow: hidden; }
.bars .b .g i { display: block; height: 100%; background: var(--exp); border-radius: 4px; }
.bars .b span:last-child { width: 90px; text-align: right; font-size: 13px; }
.notice { margin: 10px; padding: 12px 14px; border-radius: 12px; background: var(--warn-soft); color: var(--warn); font-weight: 600; display: block; width: calc(100% - 20px); text-align: left; }

/* 메모 */
.memo { padding: 12px 14px; border-bottom: 1px solid var(--line); display: flex; gap: 8px; }
.memo p { margin: 0; flex: 1; white-space: pre-wrap; word-break: break-word; }
.memo small { color: var(--muted); display: block; margin-top: 4px; }

/* 통계 */
.seg { display: inline-flex; background: var(--bg); border-radius: 10px; padding: 3px; }
.seg button { padding: 6px 14px; border-radius: 8px; color: var(--muted); font-weight: 600; }
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg.full { display: flex; } .seg.full button { flex: 1; }
.donut { display: block; margin: 16px auto 4px; width: 210px; height: 210px; }
.legend .row { min-height: 48px; }
.dot { width: 12px; height: 12px; border-radius: 6px; flex: none; }
.pct { width: 52px; flex: none; color: var(--muted); font-size: 13px; text-align: right; }

/* 자산 */
.asum { display: flex; background: var(--card); border-bottom: 1px solid var(--line); }
.asum div { flex: 1; text-align: center; padding: 12px 0; }
.asum small { display: block; color: var(--muted); font-size: 12px; }
.gh { display: flex; padding: 14px 14px 6px; color: var(--muted); font-size: 13px; font-weight: 600; }
.gh span:first-child { flex: 1; }

/* 더보기 */
.mi { display: flex; align-items: center; width: 100%; padding: 14px; border-bottom: 1px solid var(--line); text-align: left; gap: 8px; }
.mi:last-child { border-bottom: 0; }
.mi span:first-child { flex: 1; }
.mi .muted { font-size: 13px; }
.field { display: block; padding: 6px 14px; }
.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
input.in, select.in, textarea.in { width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: var(--bg); min-height: 44px; }
textarea.in { min-height: 90px; resize: vertical; }
.btns { display: flex; gap: 8px; padding: 10px 14px 14px; flex-wrap: wrap; }
.btn { flex: 1; min-height: 46px; padding: 0 14px; border-radius: 12px; background: var(--bg); border: 1px solid var(--line); font-weight: 600; white-space: nowrap; }
.btn.pri { background: var(--pri); border-color: var(--pri); color: #fff; }
.btn.del { color: var(--exp); flex: 0 0 auto; }
.btn.del.arm { background: var(--exp); border-color: var(--exp); color: #fff; }
.btn:disabled { opacity: .5; }
.help { padding: 4px 14px 12px; color: var(--muted); font-size: 13px; line-height: 1.6; }
.help ol { margin: 4px 0; padding-left: 20px; }

/* 플로팅 버튼 */
#fab { position: fixed; z-index: 6; bottom: calc(var(--nav-h) + var(--safe-b) + 14px); right: max(14px, calc(50% - 306px)); display: flex; gap: 10px; align-items: center; }
#fab button { height: 50px; border-radius: 25px; padding: 0 18px; box-shadow: var(--shadow); font-weight: 700; }
#fab .f1 { background: var(--card); color: var(--pri); border: 1px solid var(--line); }
#fab .f2 { background: var(--pri); color: #fff; }

/* 하단 메뉴 */
#nav { position: fixed; z-index: 7; bottom: 0; left: 0; right: 0; max-width: 640px; margin: 0 auto; display: flex; background: var(--card); border-top: 1px solid var(--line); padding-bottom: var(--safe-b); }
#nav button { flex: 1; height: var(--nav-h); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: var(--muted); font-size: 11px; }
#nav svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#nav svg.dots { stroke-width: 3.6; }
#nav button.on { color: var(--pri); font-weight: 700; }

/* 시트 */
.sheet-wrap { position: fixed; inset: 0; z-index: 20; display: flex; flex-direction: column; justify-content: flex-end; background: rgba(0,0,0,.4); }
.sheet { background: var(--card); width: 100%; max-width: 640px; margin: 0 auto; border-radius: 18px 18px 0 0; max-height: 94vh; overflow: auto; padding-bottom: var(--safe-b); animation: up .18s ease-out; }
@keyframes up { from { transform: translateY(40px); } }
.sh-h { display: flex; align-items: center; padding: 8px 6px 4px 14px; position: sticky; top: 0; background: var(--card); z-index: 1; }
.sh-h h2 { flex: 1; font-size: 17px; margin: 0; }
.typeseg { margin: 4px 14px 6px; }
.typeseg button.on.income { color: var(--inc); } .typeseg button.on.expense { color: var(--exp); }
.two { display: flex; gap: 8px; } .two > * { flex: 1; min-width: 0; }
.two label > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
label.btn { display: flex; align-items: center; justify-content: center; }
.catbtn { width: 100%; text-align: left; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: var(--bg); min-height: 44px; }
.catbtn.unc { color: var(--warn); border-color: var(--warn); }
.grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 6px 14px 10px; }
.grid button { min-height: 46px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line); font-size: 13px; padding: 4px; word-break: keep-all; }
.grid button.on { border-color: var(--pri); color: var(--pri); font-weight: 700; background: var(--pri-soft); }
.amt { font-size: 22px; font-weight: 700; text-align: right; }
.raw { margin: 6px 14px 4px; padding: 10px 12px; background: var(--bg); border-radius: 10px; font-size: 12px; color: var(--muted); white-space: pre-wrap; word-break: break-all; }

.chk { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.chk input { width: 22px; height: 22px; flex: none; accent-color: var(--pri); }
.chk .rm { cursor: pointer; }
.tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--warn-soft); color: var(--warn); margin-left: 4px; vertical-align: 1px; }
.tag.dup { background: var(--line); color: var(--muted); }

.catrow { display: flex; gap: 6px; align-items: center; padding: 8px 14px; border-bottom: 1px solid var(--line); }
.catrow b { width: 86px; flex: none; font-size: 14px; word-break: keep-all; }
.catrow input { flex: 1; min-width: 0; min-height: 38px; padding: 6px 10px; }
.catrow .ic { width: 34px; height: 34px; font-size: 16px; }

/* 동기화 배지 */
.sbadge { flex: none; font-size: 12px; font-weight: 700; padding: 5px 9px; border-radius: 14px; background: var(--pri-soft); color: var(--pri); white-space: nowrap; }
.sbadge.bad { background: var(--exp); color: #fff; }
.sbadge.warn { background: var(--warn-soft); color: var(--warn); }
.ok { color: #16a34a; font-weight: 600; }

/* 암호키 */
.keyslot { display: flex; justify-content: center; align-items: center; gap: 4px; padding: 18px 8px 10px; }
.keyslot span { width: 28px; height: 42px; display: inline-flex; align-items: center; justify-content: center; border-radius: 8px; background: var(--bg); border: 1px solid var(--line);
  font: 700 22px ui-monospace, "Cascadia Mono", Consolas, monospace; color: var(--muted); }
.keyslot span.done { color: var(--text); border-color: var(--pri); }
.keyslot i { font-style: normal; font-size: 22px; color: var(--muted); padding: 0 2px; }
.keyshow { text-align: center; font: 700 32px ui-monospace, "Cascadia Mono", Consolas, monospace; letter-spacing: 4px; padding: 22px 8px 12px; }
input.keyin { font: 700 22px ui-monospace, "Cascadia Mono", Consolas, monospace; letter-spacing: 3px; text-align: center; text-transform: uppercase; }

#toast { position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--safe-b) + 80px); transform: translateX(-50%); z-index: 50; background: #222; color: #fff; padding: 10px 16px; border-radius: 20px; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: 90vw; text-align: center; }
#toast.on { opacity: .95; }
