/* ─────────────────────────────────────────────────────────────
   장부 — 모바일 우선(mobile-first).
   기본 스타일이 곧 휴대폰 화면이고, 760px 이상에서 2단으로 펼친다.
   ───────────────────────────────────────────────────────────── */

:root {
  --ink:       #1B2430;
  --ink-soft:  #626E7A;
  --ink-faint: #9AA4AE;
  --paper:     #F7F7F3;
  --card:      #FFFFFF;
  --rule:      #E3E5DE;
  --rule-firm: #C9CDC4;
  --accent:    #2F6B5E;
  --accent-wash: #EAF1EE;
  --danger:    #8C3B4A;

  --display: 'Gowun Batang', serif;
  --ui: 'Pretendard Variable', Pretendard, -apple-system, system-ui, sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, monospace;

  --radius: 3px;

  /* 노치·홈바 여백. 지원하지 않는 브라우저에서는 0 으로 떨어진다. */
  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left, 0px);
  --safe-right:  env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--ui);
  font-size: 15px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;   /* 주소창 튕김 / 당겨서 새로고침 억제 */
}

/* 터치 지연 제거 + 탭 하이라이트 정리 */
button, .cell, .entry { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }

.num {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

/* 화면 전환 */
#gate, #app { display: none; }
body[data-view="gate"] #gate { display: grid; }
body[data-view="app"] #app { display: block; }

/* ── 로그인 화면 ─────────────────────────────────────────── */
.gate {
  min-height: 100dvh;
  place-items: center;
  padding: calc(20px + var(--safe-top)) calc(16px + var(--safe-right))
           calc(20px + var(--safe-bottom)) calc(16px + var(--safe-left));
}

.gate__sheet {
  width: 100%;
  max-width: 380px;
  background: var(--card);
  border: 1px solid var(--rule);
  border-top: 3px solid var(--ink);
  border-radius: var(--radius);
  padding: 30px 22px 26px;
}

.gate__eyebrow { margin: 0; font-size: 12px; letter-spacing: 0.14em; color: var(--ink-faint); }

.gate__title {
  font-family: var(--display);
  font-size: 40px;
  font-weight: 700;
  margin: 2px 0 10px;
  letter-spacing: 0.06em;
}

/* '아이디 만들기' 는 '장부' 보다 길어서 좁은 화면에서 카드를 넘친다. */
.gate__title--long { font-size: 30px; letter-spacing: 0.02em; }

.gate__lede { margin: 0 0 22px; font-size: 13.5px; color: var(--ink-soft); line-height: 1.7; }
.gate__actions { display: flex; flex-direction: column; gap: 8px; margin-top: 6px; }

/* ── 폼 요소 ─────────────────────────────────────────────── */
.field-stack { display: flex; flex-direction: column; gap: 14px; }
.field-row { display: flex; gap: 8px; }
.field-row--cats > .field { flex: 1 1 0; min-width: 0; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field--grow { flex: 1 1 auto; min-width: 0; }
.field--amount { width: 116px; flex: 0 0 auto; }

.field__label { font-size: 11.5px; letter-spacing: 0.08em; color: var(--ink-soft); }
.field__label i { font-style: normal; color: var(--ink-faint); }

input, select {
  font-family: inherit;
  /* iOS 는 16px 미만 입력칸에 포커스가 가면 화면을 확대한다. 모바일에서는 16px 고정. */
  font-size: 16px;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--rule-firm);
  border-radius: var(--radius);
  padding: 11px 10px;
  width: 100%;
  min-width: 0;
  appearance: none;
}
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--ink-soft) 50%),
                    linear-gradient(135deg, var(--ink-soft) 50%, transparent 50%);
  background-position: right 13px center, right 8px center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 24px;
}
#f-amount { font-family: var(--mono); text-align: right; }

input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn {
  font-family: inherit;
  font-size: 15px;
  min-height: 44px;           /* 손가락으로 눌리는 최소 크기 */
  padding: 11px 16px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background-color .14s ease, color .14s ease;
}
.btn--primary { background: var(--ink); color: #fff; }
.btn--primary:hover { background: var(--accent); }
.btn--primary:disabled { opacity: .55; cursor: default; }
.btn--quiet { background: transparent; color: var(--ink-soft); border-color: var(--rule-firm); }
.btn--quiet:hover { color: var(--ink); border-color: var(--ink); }
.btn--sm { min-height: 34px; padding: 6px 11px; font-size: 12.5px; }

.notice {
  margin: 0;
  font-size: 13px;
  color: var(--danger);
  border-left: 2px solid var(--danger);
  padding-left: 9px;
}
/* 오류가 아니라 알림일 때. 붉은색으로 겁주지 않는다. */
.notice--ok { color: var(--accent); border-left-color: var(--accent); }

/* ── 상단 바 (모바일에서 고정) ───────────────────────────── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 15;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(10px + var(--safe-top)) calc(14px + var(--safe-right)) 10px calc(14px + var(--safe-left));
  border-bottom: 1px solid var(--rule);
  background: var(--card);
}
.topbar__mark { font-family: var(--display); font-size: 21px; font-weight: 700; letter-spacing: 0.08em; }
.topbar__who {
  margin-left: auto;
  font-size: 12.5px;
  color: var(--ink-soft);
  max-width: 40vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar__who b { color: var(--ink); font-weight: 600; }

/* ── 레이아웃 ────────────────────────────────────────────── */
.layout {
  display: flex;
  flex-direction: column;
  gap: 12px;
  /* 하단 FAB 에 내용이 가리지 않도록 넉넉히 비워 둔다. */
  padding: 12px calc(12px + var(--safe-right)) calc(96px + var(--safe-bottom)) calc(12px + var(--safe-left));
}

.calendar, .day, .digest {
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 14px 12px;
}

/* ── 달력 ────────────────────────────────────────────────── */
.calendar__head { display: flex; align-items: center; gap: 8px; }
.calendar__title { flex: 1; text-align: center; min-width: 0; }
.calendar__month {
  font-family: var(--display);
  font-size: 22px;
  font-weight: 700;
  margin: 0;
  white-space: nowrap;
}
.calendar__total { margin: 0; font-size: 12.5px; color: var(--ink-soft); }
.calendar__total b { color: var(--ink); font-size: 14px; }

.stepper {
  background: transparent;
  border: 1px solid var(--rule-firm);
  border-radius: var(--radius);
  color: var(--ink-soft);
  width: 40px;
  height: 40px;
  flex: 0 0 auto;
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}
.stepper:hover { color: var(--ink); border-color: var(--ink); }

.weekdays, .grid { display: grid; grid-template-columns: repeat(7, 1fr); }
.weekdays {
  font-size: 11px;
  color: var(--ink-faint);
  text-align: center;
  margin-top: 12px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--rule-firm);
}
.weekdays span:first-child { color: var(--danger); }

.grid { gap: 1px; background: var(--rule); border: 1px solid var(--rule); border-top: 0; }

.cell {
  background: var(--card);
  border: 0;
  font-family: inherit;
  text-align: left;
  padding: 5px 4px 6px;
  min-height: 56px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  cursor: pointer;
  color: var(--ink);
  overflow: hidden;
}
.cell:hover { background: var(--accent-wash); }
.cell--blank { background: var(--paper); cursor: default; }
.cell--blank:hover { background: var(--paper); }
.cell--sun .cell__day { color: var(--danger); }
.cell--today .cell__day { text-decoration: underline; text-underline-offset: 3px; }
.cell--on { background: var(--accent-wash); box-shadow: inset 0 0 0 2px var(--accent); }

.cell__day { font-size: 12px; color: var(--ink-soft); font-family: var(--mono); }

/* 서명 요소: 그날 지출을 길이로 보여주는 막대 */
.cell__bar { height: 3px; background: var(--accent); opacity: .75; border-radius: 2px; }

.cell__amt {
  /* 좁은 화면에서 7칸에 금액이 들어가야 한다. 화면 폭에 따라 줄인다. */
  font-size: clamp(8.5px, 2.4vw, 12px);
  margin-top: auto;
  font-family: var(--mono);
  letter-spacing: -0.03em;
  white-space: nowrap;
}

.calendar__hint { margin: 12px 0 0; font-size: 11px; color: var(--ink-faint); }

/* ── 한눈에 보기 (기간별 대분류 집계) ────────────────────── */
.digest__head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding-bottom: 10px;
  border-bottom: 2px solid var(--ink);
}
.digest__title { font-family: var(--display); font-size: 19px; font-weight: 700; margin: 0; }
.digest__range { margin-left: auto; font-size: 11px; color: var(--ink-faint); white-space: nowrap; }

.periods { display: flex; gap: 6px; margin: 12px 0 0; }
.period {
  flex: 1 1 0;
  min-width: 0;
  min-height: 38px;
  padding: 6px 4px;
  font-family: inherit;
  font-size: 12.5px;
  white-space: nowrap;
  color: var(--ink-soft);
  background: transparent;
  border: 1px solid var(--rule-firm);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background-color .14s ease, color .14s ease;
}
.period:hover { color: var(--ink); border-color: var(--ink); }
.period[aria-selected="true"] {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}

.digest__total { margin: 14px 0 6px; font-size: 12.5px; color: var(--ink-soft); }
.digest__total b { color: var(--ink); font-size: 17px; }

.bars { list-style: none; margin: 0; padding: 0; }
.bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: baseline;
  column-gap: 10px;
  row-gap: 5px;
  padding: 9px 0;
  border-bottom: 1px solid var(--rule);
}
.bar__name { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* 비중은 이름 옆에 붙는 보조 정보라 눈에 덜 띄게 둔다 */
.bar__share { font-style: normal; font-size: 11px; color: var(--ink-faint); margin-left: 6px; }
.bar__amt { font-family: var(--mono); font-size: 14px; font-variant-numeric: tabular-nums; }
.bar__track {
  grid-column: 1 / -1;
  height: 5px;
  border-radius: 3px;
  background: var(--paper);
  overflow: hidden;
}
.bar__fill { display: block; height: 100%; background: var(--accent); opacity: .8; border-radius: 3px; }
/* 분류를 고르지 않은 내역 묶음 */
.bar--none .bar__name { color: var(--ink-faint); }
.bar--none .bar__fill { background: var(--ink-faint); }

/* ── 하루 내역 ───────────────────────────────────────────── */
/* 날짜를 고르면 여기로 스크롤한다. 고정된 상단 바에 제목이 가리지 않게 여백을 준다. */
.day { position: relative; scroll-margin-top: 60px; }

.day__head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding-bottom: 10px;
  border-bottom: 2px solid var(--ink);
}
.day__date { font-family: var(--display); font-size: 19px; font-weight: 700; margin: 0; }
.day__sum { margin-left: auto; font-size: 12.5px; color: var(--ink-soft); }
.day__sum b { font-size: 15px; color: var(--ink); }

.entries { list-style: none; margin: 0; padding: 0; }
.entry {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  column-gap: 10px;
  row-gap: 6px;
  padding: 11px 2px;
  border-bottom: 1px solid var(--rule);
}
.entry__body { min-width: 0; }
.entry__item { display: block; font-size: 15px; }
.entry__meta {
  display: block;
  font-size: 11.5px;
  color: var(--ink-faint);
  margin-top: 1px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.entry__amt { font-family: var(--mono); font-size: 15px; font-variant-numeric: tabular-nums; }

/* 모바일에서는 도구 버튼이 둘째 줄 전체 폭을 쓴다 — 손가락으로 누를 만한 크기 확보 */
.entry__tools { grid-column: 1 / -1; display: flex; gap: 6px; }
.icon-btn {
  background: transparent;
  border: 1px solid var(--rule);
  color: var(--ink-soft);
  font-family: inherit;
  font-size: 12.5px;
  min-height: 34px;
  padding: 4px 14px;
  cursor: pointer;
  border-radius: var(--radius);
}
.icon-btn:hover { color: var(--ink); background: var(--paper); }
.icon-btn--danger:hover { color: var(--danger); border-color: var(--danger); }

.empty { font-size: 13px; color: var(--ink-faint); padding: 18px 0; margin: 0; }

/* ── 입력 시트 (모바일: 아래에서 올라옴 / 데스크톱: 그냥 패널) ── */
.sheet-scrim {
  position: fixed;
  inset: 0;
  z-index: 25;
  background: rgba(27, 36, 48, .42);
  opacity: 0;
  /* 닫혀 있을 때 화면 전체를 덮은 채 탭을 먹어 버리지 않도록 반드시 꺼 둔다. */
  pointer-events: none;
  transition: opacity .2s ease;
}
body[data-sheet="open"] .sheet-scrim { opacity: 1; pointer-events: auto; }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  background: var(--card);
  border-top: 3px solid var(--ink);
  border-radius: 14px 14px 0 0;
  box-shadow: 0 -10px 30px rgba(27, 36, 48, .16);
  padding: 6px calc(16px + var(--safe-right)) calc(16px + var(--safe-bottom)) calc(16px + var(--safe-left));
  max-height: 88dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  transform: translateY(102%);
  visibility: hidden;            /* 닫혀 있을 때 탭 순서에서 빠지게 */
  transition: transform .22s ease, visibility .22s;
}
body[data-sheet="open"] .sheet { transform: none; visibility: visible; }

/* 시트 위쪽 손잡이. 눌러도 닫힌다. */
.sheet__grip {
  display: block;
  width: 100%;
  border: 0;
  background: transparent;
  padding: 8px 0 10px;
  cursor: pointer;
}
.sheet__grip::before {
  content: '';
  display: block;
  width: 40px;
  height: 4px;
  margin: 0 auto;
  border-radius: 2px;
  background: var(--rule-firm);
}

body[data-sheet="open"] { overflow: hidden; }

.editor { display: flex; flex-direction: column; gap: 13px; }
.editor__head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding-bottom: 9px;
  border-bottom: 1px solid var(--rule-firm);
}
.editor__title { font-size: 12px; letter-spacing: 0.1em; color: var(--ink-soft); margin: 0; font-weight: 500; }
.editor__on { margin-left: auto; font-size: 12.5px; color: var(--ink); font-family: var(--mono); }
.editor__actions { display: flex; gap: 8px; }
.editor__actions .btn { flex: 1; }

/* 좁은 화면에서는 시트를 닫는 버튼이므로 늘 있어야 한다. */
#cancel-edit { display: block; }

/* ── 추가 버튼 (모바일 전용) ─────────────────────────────── */
.fab {
  position: fixed;
  right: calc(16px + var(--safe-right));
  bottom: calc(16px + var(--safe-bottom));
  z-index: 20;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 0;
  background: var(--ink);
  color: #fff;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(27, 36, 48, .3);
}
.fab:hover { background: var(--accent); }
body[data-sheet="open"] .fab { opacity: 0; pointer-events: none; }

/* ─────────────────────────────────────────────────────────────
   넓은 화면 — 2단으로 펼치고, 시트는 오른쪽 칸에 붙박이 패널이 된다.
   ───────────────────────────────────────────────────────────── */
@media (min-width: 760px) {
  html, body { font-size: 15px; }

  .topbar { position: static; padding: 14px 22px; }
  .topbar__mark { font-size: 22px; }
  .topbar__who { font-size: 13px; max-width: none; }

  .layout {
    display: grid;
    grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr);
    gap: 26px;
    max-width: 1080px;
    margin: 0 auto;
    padding: 26px 22px 60px;
    align-items: start;
  }

  /* 왼쪽에 달력·요약을 쌓고, 오른쪽 내역 칸이 두 줄을 통째로 차지한다.
     이렇게 두어야 DOM 순서(달력 → 요약 → 내역)를 좁은 화면에 맞춰 두고도
     넓은 화면에서 요약이 오른쪽으로 튀지 않는다. */
  .calendar { grid-column: 1; grid-row: 1; }
  .digest   { grid-column: 1; grid-row: 2; }
  .day      { grid-column: 2; grid-row: 1 / span 2; }

  .calendar, .day, .digest { padding: 20px; }
  .digest__title { font-size: 21px; }
  .digest__range { font-size: 11.5px; }
  .calendar__month { font-size: 24px; }
  .stepper { width: 32px; height: 32px; font-size: 15px; }
  .cell { min-height: 74px; padding: 7px 8px 9px; }
  .cell__day { font-size: 12.5px; }
  .cell__amt { font-size: 12px; letter-spacing: -0.01em; }
  .day__date { font-size: 21px; }

  input, select { font-size: 14.5px; padding: 9px 10px; }
  .btn { font-size: 14px; min-height: 0; padding: 10px 16px; }
  .btn--sm { min-height: 0; }
  .field-row { gap: 10px; }
  .field--amount { width: 120px; }

  /* 내역 한 줄에 도구까지 나란히 */
  .entry { display: flex; gap: 10px; }
  .entry__body { flex: 1; }
  .entry__item, .entry__amt { font-size: 14.5px; }
  .entry__tools { gap: 4px; }
  .icon-btn { border-color: transparent; min-height: 0; padding: 4px 6px; font-size: 12px; }

  /* 시트 → 오른쪽 칸 안의 평범한 패널 */
  .sheet {
    position: static;
    transform: none;
    visibility: visible;
    max-height: none;
    overflow: visible;
    z-index: auto;
    background: transparent;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    padding: 0;
    margin-top: 22px;
    transition: none;
  }
  .sheet__grip, .sheet-scrim, .fab { display: none; }
  body[data-sheet="open"] { overflow: auto; }

  /* 늘 펼쳐져 있으니 닫을 것이 없다. 수정 중일 때만 '수정 그만두기'로 나타난다. */
  #cancel-edit { display: none; }
  body[data-editing="1"] #cancel-edit { display: block; }
}

@media (prefers-reduced-motion: reduce) {
  *, .sheet, .sheet-scrim { transition: none !important; }
}
