/* 둘이서 일정·가계부
   레이아웃: 상단 월 이동 바(고정) → 본문(달력 | 가계부 | 설정) → 하단 탭바(고정).
   모바일 우선, 데스크톱은 가운데 960px. 색은 전부 토큰, 다크 모드는 OS 설정을 따름. */

:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efede7;
  --fg: #1f1e1b;
  --muted: #7d796f;
  --line: #e3e0d8;
  --p1: #e0734f;   --p1-soft: #fbe5dc;   /* 사람 1 */
  --p2: #3f8fcf;   --p2-soft: #dbeaf8;   /* 사람 2 */
  --both: #8b6fd0; --both-soft: #e8e1f7; /* 함께 */
  --expense: #c9423a;
  --income: #2b78c2;
  --ok: #3b9b66;
  --sun: #d14b43;
  --sat: #3a7dc9;
  --shadow: 0 12px 32px rgba(31, 30, 27, .14);
  --radius: 14px;
  --font: "Pretendard Variable", Pretendard, "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15161a; --surface: #1e2026; --surface-2: #272a31; --fg: #ecebe6; --muted: #9b988f; --line: #2f323b;
    --p1: #e98a68; --p1-soft: #3f2b23; --p2: #5fa3dd; --p2-soft: #1f3142; --both: #a48ee0; --both-soft: #2f2942;
    --expense: #e26a62; --income: #5c9fe0; --ok: #5cb882; --sun: #e2706a; --sat: #6aa0e0;
    --shadow: 0 12px 32px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15161a; --surface: #1e2026; --surface-2: #272a31; --fg: #ecebe6; --muted: #9b988f; --line: #2f323b;
  --p1: #e98a68; --p1-soft: #3f2b23; --p2: #5fa3dd; --p2-soft: #1f3142; --both: #a48ee0; --both-soft: #2f2942;
  --expense: #e26a62; --income: #5c9fe0; --ok: #5cb882; --sun: #e2706a; --sat: #6aa0e0;
  --shadow: 0 12px 32px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; font-family: var(--font); font-size: 15px; line-height: 1.45;
  background: var(--bg); color: var(--fg); -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { line-height: 1.3; }
.num { font-variant-numeric: tabular-nums; }
:focus-visible { outline: 2px solid var(--p2); outline-offset: 2px; }
[hidden] { display: none !important; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* ---------- 뼈대 ---------- */
.app { max-width: 960px; margin: 0 auto; min-height: 100%; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 5; background: var(--bg);
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 8px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.month-nav { display: flex; align-items: center; gap: 4px; }
.month-nav h1 { font-size: 19px; font-weight: 700; margin: 0 6px; min-width: 6.5em; text-align: center; letter-spacing: -.01em; }
.top-right { display: flex; align-items: center; gap: 10px; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
  display: inline-grid; place-items: center; font-size: 20px; line-height: 1; padding: 0 0 2px;
}
.pill-btn { height: 32px; padding: 0 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); font-size: 13px; font-weight: 600; }
.sync { font-size: 12px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.sync::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.sync.ok::before { background: var(--ok); }
.sync.err::before { background: var(--expense); }

main { flex: 1; padding: 0 16px calc(96px + env(safe-area-inset-bottom, 0px)); }

/* ---------- 달력 ---------- */
.cal-head { display: grid; grid-template-columns: repeat(7, 1fr); font-size: 12px; color: var(--muted); text-align: center; padding: 4px 0 6px; }
.cal-head .sun { color: var(--sun); }
.cal-head .sat { color: var(--sat); }
.cal {
  display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 1px;
  background: var(--line); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
}
.cell {
  background: var(--surface); border: 0; min-height: 86px; min-width: 0; padding: 4px 3px 3px;
  display: flex; flex-direction: column; gap: 2px; text-align: left; font: inherit; color: inherit;
}
.cell:hover { background: var(--surface-2); }
.cell.other { opacity: .45; }
.cell > span { display: block; min-width: 0; }
.cell .d { display: flex; align-items: center; gap: 4px; height: 20px; font-size: 12px; font-weight: 600; }
.cell .d .n { width: 20px; height: 20px; display: inline-grid; place-items: center; border-radius: 50%; flex: none; }
.cell.today .d .n { background: var(--fg); color: var(--bg); }
.cell.sun .d, .cell.hol .d { color: var(--sun); }
.cell.sat .d { color: var(--sat); }
.cell .hname { font-size: 10px; line-height: 1.2; font-weight: 500; color: var(--sun); padding-left: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip {
  font-size: 11px; line-height: 1.35; padding: 1px 4px; border-radius: 4px; border-left: 3px solid;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chip b { font-weight: 600; opacity: .8; }
.chip.p1 { background: var(--p1-soft); border-color: var(--p1); }
.chip.p2 { background: var(--p2-soft); border-color: var(--p2); }
.chip.both { background: var(--both-soft); border-color: var(--both); }
.cell .more { font-size: 10px; color: var(--muted); padding-left: 4px; }
.cell .spend { margin-top: auto; font-size: 10px; color: var(--expense); text-align: right; }
.legend { font-size: 12px; color: var(--muted); margin: 10px 2px 0; }
@media (min-width: 700px) {
  .cell { min-height: 112px; padding: 6px; }
  .chip { font-size: 12px; }
  .cell .spend, .cell .more { font-size: 11px; }
}

.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; vertical-align: 1px; background: var(--muted); }
.dot.p1 { background: var(--p1); } .dot.p2 { background: var(--p2); } .dot.both { background: var(--both); }
.mark { width: 4px; align-self: stretch; border-radius: 2px; background: var(--muted); }
.mark.p1 { background: var(--p1); } .mark.p2 { background: var(--p2); } .mark.both { background: var(--both); }

/* ---------- 가계부 ---------- */
.summary { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin: 6px 0 10px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; min-width: 0; }
.stat .k { font-size: 12px; color: var(--muted); }
.stat .v { font-size: 17px; font-weight: 700; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat.income .v { color: var(--income); }
.stat.expense .v { color: var(--expense); }
@media (min-width: 700px) { .stat .v { font-size: 22px; } }
.who-split { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin: 0 2px 14px; }
.cats { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 14px; }
.cats h2 { font-size: 13px; color: var(--muted); font-weight: 600; margin: 0 0 8px; }
.cats .list { display: grid; gap: 8px; }
.cat-row { display: grid; grid-template-columns: 6em minmax(0, 1fr) 7em; align-items: center; gap: 8px; font-size: 13px; }
.cat-row .c { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cat-row .r { text-align: right; }
.bar { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--expense); border-radius: 4px; }
.filters { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.filters .sep { width: 1px; height: 18px; background: var(--line); margin: 0 4px; }
.chipbtn { height: 30px; padding: 0 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); font-size: 13px; }
.chipbtn.on { background: var(--fg); color: var(--bg); border-color: var(--fg); }
.tx-group { margin-bottom: 14px; }
.tx-group h3 { font-size: 13px; color: var(--muted); font-weight: 600; margin: 0 2px 6px; display: flex; justify-content: space-between; }
.tx-group .items { display: grid; gap: 6px; }

.row, .tx {
  display: grid; grid-template-columns: 4px minmax(0, 1fr) auto; gap: 10px; align-items: center;
  width: 100%; min-height: 50px; text-align: left; border-radius: 10px; padding: 9px 12px; font: inherit; color: inherit;
}
.row { background: var(--surface-2); border: 0; }
.tx { background: var(--surface); border: 1px solid var(--line); }
.row:hover, .tx:hover { filter: brightness(.97); }
.row .body, .tx .body { display: grid; min-width: 0; }
.row .t, .tx .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .s, .tx .s { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx .amt { font-weight: 700; white-space: nowrap; }
.tx.expense .amt { color: var(--expense); }
.tx.income .amt { color: var(--income); }
.empty { color: var(--muted); font-size: 13px; padding: 10px 2px; }

/* ---------- 설정 ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin: 8px 0 12px; display: grid; gap: 12px; }
.card h2 { margin: 0; font-size: 15px; }
.hint { font-size: 12px; color: var(--muted); margin: 0; word-break: break-all; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (max-width: 440px) { .card .two { grid-template-columns: 1fr; } }

/* ---------- 폼 ---------- */
.form { display: grid; gap: 12px; }
.field { display: grid; gap: 6px; min-width: 0; }
.field label { font-size: 12px; color: var(--muted); font-weight: 600; }
.field input, .field select, .field textarea {
  width: 100%; height: 42px; padding: 0 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); min-width: 0;
}
.field textarea { height: auto; padding: 10px 12px; min-height: 60px; resize: vertical; }
.field input[type="date"], .field input[type="time"] { -webkit-appearance: none; appearance: none; }
.amount-wrap { display: flex; align-items: center; gap: 8px; }
.amount-wrap input { flex: 1; font-size: 22px; font-weight: 700; text-align: right; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--surface); }
.seg button { flex: 1; height: 40px; border: 0; background: transparent; font-weight: 600; color: var(--muted); min-width: 0; padding: 0 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--fg); color: var(--bg); }
.seg.who button.on[data-value="p1"] { background: var(--p1); color: #fff; }
.seg.who button.on[data-value="p2"] { background: var(--p2); color: #fff; }
.seg.who button.on[data-value="both"] { background: var(--both); color: #fff; }
.seg.type button.on[data-value="expense"] { background: var(--expense); color: #fff; }
.seg.type button.on[data-value="income"] { background: var(--income); color: #fff; }
.actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 4px; }
.btn { height: 40px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-weight: 600; white-space: nowrap; }
.btn.primary { background: var(--fg); color: var(--bg); border-color: var(--fg); }
.btn.danger { color: var(--expense); margin-right: auto; }
.btn.danger-bg { background: var(--expense); border-color: var(--expense); color: #fff; }

/* ---------- 다이얼로그 / 시트 ---------- */
dialog {
  border: 0; padding: 0; background: var(--surface); color: var(--fg); border-radius: 18px; box-shadow: var(--shadow);
  width: min(520px, calc(100% - 32px)); max-height: 85vh; max-height: 85dvh; overflow: auto;
}
dialog::backdrop { background: rgba(18, 16, 12, .48); }
.sheet { padding: 18px 18px calc(18px + env(safe-area-inset-bottom, 0px)); }
.sheet h2 { margin: 0 0 12px; font-size: 17px; }
.sheet h2 small { color: var(--sun); font-weight: 500; font-size: 13px; margin-left: 8px; }
.sheet .sub { font-size: 12px; color: var(--muted); font-weight: 600; margin: 12px 0 6px; }
.sheet .list { display: grid; gap: 6px; }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 16px; }
.confirm-msg { font-size: 15px; margin: 4px 0 16px; }
@media (max-width: 600px) {
  dialog { width: 100%; max-width: 100%; margin: auto 0 0; border-radius: 18px 18px 0 0; max-height: 92vh; max-height: 92dvh; }
}

/* ---------- 떠 있는 버튼 / 탭바 / 토스트 ---------- */
.fab {
  position: fixed; right: 16px; bottom: calc(72px + env(safe-area-inset-bottom, 0px)); z-index: 6;
  width: 54px; height: 54px; border-radius: 50%; border: 0; background: var(--fg); color: var(--bg);
  font-size: 28px; line-height: 1; box-shadow: var(--shadow); padding: 0 0 2px;
}
@media (min-width: 960px) { .fab { right: calc(50% - 480px + 16px); } }
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 7; background: var(--surface); border-top: 1px solid var(--line);
  display: flex; justify-content: center; padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tabbar button { flex: 1; max-width: 320px; height: 54px; border: 0; background: transparent; font-size: 13px; font-weight: 600; color: var(--muted); }
.tabbar button.on { color: var(--fg); box-shadow: inset 0 3px 0 var(--fg); }
.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); z-index: 20;
  background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 13px; white-space: nowrap;
  opacity: 0; transition: opacity .2s; pointer-events: none;
}
.toast.show { opacity: 1; }
