/* ===== 기본 토큰 ===== */
:root {
  --color-bg: #f6f7fb;
  --color-surface: #ffffff;
  --color-border: #e5e7eb;
  --color-text: #111827;
  --color-text-muted: #6b7280;
  --color-primary: #2563eb;
  --color-primary-dark: #1d4ed8;
  --color-income: #059669;
  --color-expense: #dc2626;
  --color-danger: #dc2626;
  --color-warn: #f59e0b;
  --color-shadow: rgba(15, 23, 42, 0.08);

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;

  --tabbar-h: 64px;
  --header-h: 56px;

  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo",
    "Malgun Gothic", "맑은 고딕", "Noto Sans KR", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

button { font: inherit; cursor: pointer; }

/* ===== 헤더 ===== */
.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  padding-top: var(--safe-top);
}
.app-header__inner {
  max-width: 960px;
  margin: 0 auto;
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
}
.app-header__title {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
}
.desktop-nav { display: none; gap: 4px; margin-left: 16px; }
.desktop-nav__btn {
  background: none;
  border: 0;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  font-weight: 500;
  min-height: 36px;
}
.desktop-nav__btn:hover { background: var(--color-bg); }
.desktop-nav__btn.is-active {
  background: var(--color-primary);
  color: #fff;
}
.desktop-only { display: none; margin-left: auto; }

/* ===== 본문 ===== */
.app-main {
  max-width: 960px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 96px);
}
.tab-panel { display: block; }
.tab-panel[hidden] { display: none; }

.section-title {
  margin: 24px 0 12px;
  font-size: 15px;
  font-weight: 600;
  color: var(--color-text-muted);
}

/* ===== 월 선택기 ===== */
.month-picker {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin: 4px 0 16px;
}
.month-picker__btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  font-size: 14px;
}
.month-picker__label {
  font-size: 20px;
  font-weight: 700;
  min-width: 96px;
  text-align: center;
}

/* ===== 요약 카드 ===== */
.summary-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.summary-card {
  background: var(--color-surface);
  padding: 14px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
  box-shadow: 0 1px 2px var(--color-shadow);
}
.summary-card__label {
  font-size: 12px;
  color: var(--color-text-muted);
  margin-bottom: 4px;
}
.summary-card__value {
  font-size: 18px;
  font-weight: 700;
  word-break: break-all;
}
.summary-card--income .summary-card__value { color: var(--color-income); }
.summary-card--expense .summary-card__value { color: var(--color-expense); }

/* ===== 차트 ===== */
.chart-section {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 12px;
  margin-top: 8px;
}
.chart-wrap {
  position: relative;
  width: 100%;
  height: 320px;
}
#category-chart {
  width: 100%;
  height: 100%;
  display: block;
}
.chart-empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
  font-size: 14px;
}

/* ===== 거래 목록 ===== */
.tx-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.tx-list--full { padding-bottom: 16px; }

/* 거래 항목 (스와이프 컨테이너) */
.tx-item {
  position: relative;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
  min-height: 64px;
  touch-action: pan-y;
  --swipe-progress: 0;
}
/* 가로 드래그 시작 시 브라우저의 가로 제스처 가로채기 차단 */
.tx-item.is-dragging { touch-action: none; }
.tx-item.is-dragging .tx-item__content { touch-action: none; }

/* 스와이프 액션 힌트(배경 표시 전용, 클릭 불가) */
.tx-item__hint {
  position: absolute;
  top: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  padding: 0 18px;
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.02em;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease, background-color 0.15s ease;
}
.tx-item__hint--edit {
  left: 0;
  right: 50%;
  background: var(--color-primary);
  justify-content: flex-start;
}
.tx-item__hint--delete {
  right: 0;
  left: 50%;
  background: var(--color-danger);
  justify-content: flex-end;
}
.tx-item__hint-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.tx-item__hint-label::before {
  display: inline-block;
  font-size: 16px;
  line-height: 1;
}
.tx-item__hint--edit .tx-item__hint-label::before { content: "✎"; }
.tx-item__hint--delete .tx-item__hint-label::before { content: "🗑"; }

/* 진행 중 방향에 따라 해당 힌트만 점진 노출 */
.tx-item.is-swiping-right .tx-item__hint--edit {
  opacity: calc(0.35 + var(--swipe-progress) * 0.65);
}
.tx-item.is-swiping-left .tx-item__hint--delete {
  opacity: calc(0.35 + var(--swipe-progress) * 0.65);
}

/* 임계점 통과 강조: 색을 짙게, 레이블 굵게, 약간 살짝 펄스 */
.tx-item.is-past-threshold .tx-item__hint {
  filter: brightness(1.1) saturate(1.15);
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.35);
}
.tx-item.is-past-threshold.is-swiping-left .tx-item__hint--delete .tx-item__hint-label,
.tx-item.is-past-threshold.is-swiping-right .tx-item__hint--edit .tx-item__hint-label {
  transform: scale(1.08);
  font-weight: 800;
}
.tx-item__hint-label { transition: transform 0.12s ease; }

.tx-item__content {
  position: relative;
  background: var(--color-surface);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  min-height: 64px;
  transition: transform 0.18s ease;
  will-change: transform;
  user-select: none;
}
.tx-item.is-dragging .tx-item__content { transition: none; }
.tx-item__main {
  flex: 1;
  min-width: 0;
}
.tx-item__line1 {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}
.tx-item__category {
  font-weight: 600;
}
.tx-item__date {
  color: var(--color-text-muted);
  font-size: 12px;
}
.tx-item__memo {
  font-size: 13px;
  color: var(--color-text-muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tx-item__amount {
  font-weight: 700;
  font-size: 15px;
  white-space: nowrap;
}
.tx-item__amount--income { color: var(--color-income); }
.tx-item__amount--expense { color: var(--color-expense); }

/* ===== 빈 상태 ===== */
.empty-state {
  text-align: center;
  color: var(--color-text-muted);
  padding: 32px 16px;
  font-size: 14px;
}

/* ===== 필터 ===== */
.tx-filters {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}
.filter-row {
  display: flex;
  gap: 8px;
}
.filter-row > * { flex: 1; }

/* ===== 카테고리 ===== */
.cat-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.cat-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 999px;
  padding: 8px 12px;
  min-height: 36px;
  font-size: 14px;
}
.cat-chip__type {
  font-size: 11px;
  color: var(--color-text-muted);
  padding: 2px 6px;
  background: var(--color-bg);
  border-radius: 999px;
}
.cat-chip__remove {
  background: none;
  border: 0;
  color: var(--color-text-muted);
  font-size: 16px;
  padding: 0 4px;
  min-width: 28px;
  min-height: 28px;
}
.cat-chip__remove:hover { color: var(--color-danger); }
.cat-add-row {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}
.cat-add-row .input { flex: 1; min-width: 120px; }
.cat-add-row .btn { min-width: 80px; }

/* ===== 폼 입력 ===== */
.input {
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  font-size: 16px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  color: var(--color-text);
}
.input:focus {
  outline: 2px solid var(--color-primary);
  outline-offset: 0;
}
textarea.input { min-height: 96px; font-family: inherit; }

/* ===== 버튼 ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 14px;
  background: var(--color-surface);
  color: var(--color-text);
  cursor: pointer;
}
.btn--primary { background: var(--color-primary); color: #fff; }
.btn--primary:hover { background: var(--color-primary-dark); }
.btn--danger { background: var(--color-danger); color: #fff; }
.btn--ghost { background: var(--color-surface); border-color: var(--color-border); color: var(--color-text); }
.btn--ghost:hover { background: var(--color-bg); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row--end { justify-content: flex-end; }

/* ===== 페이지네이션 ===== */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 12px;
}

/* ===== 하단 탭바 ===== */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  display: flex;
  z-index: 40;
}
.tabbar__btn {
  flex: 1;
  background: none;
  border: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--color-text-muted);
  font-size: 11px;
  min-height: 44px;
}
.tabbar__btn.is-active { color: var(--color-primary); }
.tabbar__icon { font-size: 18px; line-height: 1; }
.tabbar__label { font-size: 11px; }

/* ===== FAB ===== */
.fab {
  position: fixed;
  right: 16px;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px);
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 0;
  background: var(--color-primary);
  color: #fff;
  font-size: 28px;
  line-height: 1;
  box-shadow: 0 6px 16px rgba(37, 99, 235, 0.4);
  z-index: 35;
  display: none;
}
.fab.is-visible { display: flex; align-items: center; justify-content: center; }
.fab.is-hidden { display: none !important; }

/* ===== 모달 (모바일: 풀스크린) ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
}
.modal[hidden] { display: none; }
.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
}
.modal__panel {
  position: relative;
  background: var(--color-surface);
  width: 100%;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}
.modal__header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
  position: sticky;
  top: 0;
  background: var(--color-surface);
  z-index: 1;
}
.modal__close {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: none;
  border: 0;
  font-size: 18px;
  color: var(--color-text);
}
.modal__title { flex: 1; margin: 0; font-size: 16px; font-weight: 600; }
.modal__save { min-width: 64px; }
.modal__form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
}

.field { display: flex; flex-direction: column; gap: 6px; }
.field__label { font-size: 13px; color: var(--color-text-muted); font-weight: 500; }
.field__label small { color: var(--color-text-muted); font-weight: normal; }

.seg {
  display: flex;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.seg__btn {
  flex: 1;
  min-height: 48px;
  border: 0;
  background: var(--color-surface);
  color: var(--color-text-muted);
  font-weight: 600;
}
.seg__btn[aria-pressed="true"] {
  background: var(--color-primary);
  color: #fff;
}

/* 확인 다이얼로그 */
.modal--confirm { align-items: center; justify-content: center; padding: 24px; }
.modal__panel--small {
  max-width: 360px;
  border-radius: var(--radius-lg);
  padding: 20px;
  height: auto;
}
.confirm__title { margin: 0 0 8px; font-size: 16px; }
.confirm__message { margin: 0 0 16px; font-size: 14px; color: var(--color-text-muted); }

/* ===== 토스트 ===== */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 88px);
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.92);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 200;
  max-width: 90vw;
  text-align: center;
}

/* Undo 토스트 (5초 한정, 되돌리기 버튼 포함) */
.toast--undo {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  padding: 10px 8px 10px 16px;
}
.toast--undo[hidden] { display: none; }
.toast__btn {
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  border: 0;
  border-radius: 999px;
  padding: 6px 14px;
  font-weight: 700;
  font-size: 13px;
  min-height: 32px;
  cursor: pointer;
}
.toast__btn:hover { background: rgba(255, 255, 255, 0.28); }

/* ===== 예산 진척 카드 ===== */
.budget-progress {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  margin-top: 12px;
  box-shadow: 0 1px 2px var(--color-shadow);
}
.budget-progress[hidden] { display: none; }
.budget-progress__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 8px;
}
.budget-progress__title {
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text-muted);
}
.budget-progress__pct {
  font-size: 16px;
  font-weight: 700;
  color: var(--color-text);
}
.budget-progress__bar {
  position: relative;
  width: 100%;
  height: 10px;
  background: var(--color-bg);
  border-radius: 999px;
  overflow: hidden;
}
.budget-progress__fill {
  height: 100%;
  background: var(--color-primary);
  border-radius: 999px;
  transition: width 0.25s ease, background-color 0.25s ease;
}
.budget-progress.is-warn .budget-progress__fill { background: var(--color-warn); }
.budget-progress.is-warn .budget-progress__pct { color: var(--color-warn); }
.budget-progress.is-over .budget-progress__fill { background: var(--color-danger); }
.budget-progress.is-over .budget-progress__pct { color: var(--color-danger); }
.budget-progress__text {
  margin-top: 6px;
  font-size: 13px;
  color: var(--color-text-muted);
}

/* ===== 예산 설정 섹션 ===== */
.budget-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.budget-month-label {
  font-size: 13px;
  color: var(--color-text-muted);
}
.budget-month-label .hint { display: block; margin-top: 2px; }
.budget-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.budget-row__label {
  font-size: 13px;
  color: var(--color-text-muted);
  font-weight: 500;
}
.budget-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  flex-wrap: wrap;
}
.budget-toggle input[type="checkbox"] {
  width: 20px;
  height: 20px;
  margin: 0;
}
.budget-toggle .hint { flex-basis: 100%; }
.budget-subtitle {
  margin: 8px 0 0;
  font-size: 14px;
  font-weight: 600;
}
.budget-cat-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.budget-cat-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.budget-cat-row__name {
  flex: 1;
  font-size: 14px;
  min-width: 0;
}
.budget-cat-row .input {
  flex: 0 0 140px;
  min-height: 40px;
}

/* ===== 다중 선택: 항목 선택 표시 ===== */
.tx-item.is-selected .tx-item__content {
  background: #e0e7ff;
  box-shadow: inset 3px 0 0 var(--color-primary);
}
.tx-item__select-mark {
  display: none;
  width: 22px;
  height: 22px;
  flex: 0 0 22px;
  border-radius: 50%;
  border: 2px solid var(--color-border);
  background: var(--color-surface);
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: transparent;
}
body.is-multiselect .tx-item__select-mark { display: inline-flex; }
.tx-item.is-selected .tx-item__select-mark {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
}
/* 다중 선택 모드 중에는 스와이프 힌트가 보이지 않도록 */
body.is-multiselect .tx-item__hint { display: none; }

/* ===== 컨텍스트 액션바 ===== */
.action-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding: 8px 12px calc(8px + var(--safe-bottom));
  background: var(--color-primary);
  color: #fff;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 50;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.15);
}
.action-bar[hidden] { display: none; }
.action-bar__close {
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  border: 0;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  font-size: 16px;
  cursor: pointer;
}
.action-bar__count {
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
}
.action-bar__group {
  display: flex;
  gap: 4px;
}
.action-bar__text-btn {
  background: transparent;
  color: #fff;
  border: 0;
  padding: 6px 10px;
  font-size: 12px;
  border-radius: 6px;
  cursor: pointer;
  min-height: 36px;
}
.action-bar__text-btn:hover { background: rgba(255, 255, 255, 0.15); }
.action-bar__spacer { flex: 1; }
.action-bar__action-btn {
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  border: 0;
  border-radius: 8px;
  padding: 8px 12px;
  font-weight: 700;
  font-size: 13px;
  min-height: 40px;
  cursor: pointer;
  white-space: nowrap;
}
.action-bar__action-btn--danger { background: var(--color-danger); }
.action-bar__action-btn--danger:hover { background: #b91c1c; }
.action-bar__action-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* 모바일: 액션바가 하단 탭바 자리를 일시 대체 */
body.is-multiselect .tabbar { display: none; }
body.is-multiselect .fab { display: none !important; }

/* ===== 카테고리 선택 시트 ===== */
.cat-picker__hint {
  padding: 12px 16px;
  font-size: 13px;
  color: var(--color-text-muted);
  border-bottom: 1px solid var(--color-border);
}
.cat-picker__list {
  list-style: none;
  margin: 0;
  padding: 8px 0;
}
.cat-picker__item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  background: none;
  border: 0;
  text-align: left;
  padding: 14px 16px;
  font-size: 15px;
  min-height: 48px;
  cursor: pointer;
  color: var(--color-text);
  border-bottom: 1px solid var(--color-border);
}
.cat-picker__item:hover { background: var(--color-bg); }
.cat-picker__item:last-child { border-bottom: 0; }
.cat-picker__item-type {
  font-size: 11px;
  color: var(--color-text-muted);
  padding: 2px 6px;
  background: var(--color-bg);
  border-radius: 999px;
}

.hint { font-size: 13px; color: var(--color-text-muted); line-height: 1.6; }

/* ===== 반응형: 데스크톱 (>= 768px) ===== */
@media (min-width: 768px) {
  .desktop-nav { display: flex; }
  .desktop-only { display: inline-flex; }

  .tabbar { display: none; }
  .fab { display: none !important; }

  .app-main {
    padding: 24px;
    padding-bottom: 48px;
  }

  .summary-cards { grid-template-columns: repeat(3, 1fr); gap: 16px; }
  .summary-card { padding: 18px; }
  .summary-card__value { font-size: 22px; }

  /* 데스크톱 모달: 중앙 다이얼로그 */
  .modal { align-items: center; justify-content: center; padding: 24px; }
  .modal__panel {
    width: 480px;
    max-width: 100%;
    height: auto;
    max-height: 90vh;
    border-radius: var(--radius-lg);
    overflow: hidden;
  }
  .modal__header { border-top-left-radius: var(--radius-lg); border-top-right-radius: var(--radius-lg); }

  .toast { bottom: 32px; }
  .toast--undo { bottom: 32px; }

  .tx-filters { flex-direction: row; align-items: center; flex-wrap: wrap; }
  .tx-filters .filter-row { flex: 1; min-width: 280px; }

  /* 데스크톱: 컨텍스트 액션바는 헤더 아래 상단에 고정 */
  .action-bar {
    top: var(--header-h);
    bottom: auto;
    height: 56px;
    padding: 8px 16px;
    max-width: 960px;
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    border-radius: 0 0 var(--radius-md) var(--radius-md);
  }
  body.is-multiselect .app-main { padding-top: calc(24px + 56px); }
}
