/* CardPulse 공통 스타일 - DESIGN_2.md 「CardPulse 적용 규칙」 기준.
   모바일 우선으로 작성하고, PC 배치는 @media (min-width: 768px) 안에만 둔다.
   색상·크기 값은 tokens.css 변수만 사용한다. */

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--color-canvas);
  color: var(--color-ink);
  font-family: var(--font-sans);
  font-size: var(--text-body);
  line-height: 1.43;
  overflow-x: hidden;
}
a { color: inherit; }
h1, h2, h3 { margin: 0; letter-spacing: -0.02em; }
.hidden { display: none !important; }
.muted { color: var(--color-mid-gray); }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.neg { color: var(--color-mid-gray); }
.icon { width: 20px; height: 20px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- 헤더 / 메뉴 ---------- */
.app-header {
  position: sticky; top: 0; z-index: 20;
  background: var(--color-paper);
  border-bottom: 1px solid var(--color-hairline);
}
.app-header-inner {
  max-width: var(--page-max-width);
  margin: 0 auto;
  height: var(--nav-height);
  padding: 0 var(--page-gutter);
  display: flex; align-items: center; gap: 16px;
}
.brand { font-weight: var(--font-weight-semibold); font-size: 17px; letter-spacing: -0.02em; text-decoration: none; }
.header-actions { margin-left: auto; display: flex; align-items: center; gap: 4px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: var(--touch-min); height: var(--touch-min);
  padding: 0 10px; gap: 6px;
  border: 0; background: transparent; color: var(--color-mid-gray);
  border-radius: var(--radius-control);
  font: inherit; cursor: pointer; text-decoration: none;
}
.icon-btn:hover, .icon-btn.active { color: var(--color-ink); background: var(--color-canvas); }
.icon-btn .label { display: none; }
.logout-form { margin: 0; }

/* 모바일: 하단 탭 */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: calc(var(--nav-height) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--color-paper);
  border-top: 1px solid var(--color-hairline);
  display: grid; grid-template-columns: repeat(4, 1fr);
}
.tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--color-mid-gray); text-decoration: none;
  font-size: var(--text-caption);
}
.tab .icon { width: 24px; height: 24px; }
.tab.active { color: var(--color-ink); font-weight: var(--font-weight-medium); }

/* ---------- 페이지 ---------- */
.page {
  max-width: var(--page-max-width);
  margin: 0 auto;
  padding: var(--section-gap) var(--page-gutter);
  padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom) + var(--section-gap));
  display: flex; flex-direction: column; gap: var(--card-gap);
}
.page-title { font-size: var(--text-page-title); font-weight: var(--font-weight-semibold); line-height: 1.2; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--color-mid-gray); }
.grid { display: grid; gap: var(--card-gap); grid-template-columns: 1fr; }

/* ---------- 카드 ---------- */
.card {
  background: var(--color-paper);
  border: 1px solid var(--color-hairline);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  padding: var(--card-padding);
  min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.card-title { font-size: var(--text-card-title); font-weight: var(--font-weight-semibold); }
.card-sub { color: var(--color-mid-gray); font-size: var(--text-caption); }

/* 합계 숫자 */
.stat-label { font-size: var(--text-caption); color: var(--color-mid-gray); }
.stat-value { font-size: var(--text-stat); font-weight: var(--font-weight-semibold); letter-spacing: -0.02em; line-height: 1.15; }
.stat-sub { color: var(--color-mid-gray); margin-top: 2px; }
.delta { font-weight: var(--font-weight-medium); color: var(--color-ink); }

/* ---------- 버튼 / 입력 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--touch-min);
  padding: 0 16px;
  border: 0; border-radius: var(--radius-control);
  font: inherit; font-weight: var(--font-weight-medium);
  cursor: pointer; text-decoration: none; white-space: nowrap;
  background: var(--color-canvas); color: var(--color-ink);
}
.btn-primary { background: var(--color-ink); color: #fafafa; }
.btn-outline { background: transparent; box-shadow: inset 0 0 0 1px var(--color-hairline); }
.btn-danger { background: transparent; color: var(--color-danger); box-shadow: inset 0 0 0 1px var(--color-hairline); }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: 13px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: var(--text-caption); color: var(--color-mid-gray); }
.input, .select {
  width: 100%;
  min-height: var(--touch-min);
  padding: 8px 14px;
  border: 1px solid transparent; border-radius: var(--radius-control);
  background: var(--color-canvas); color: var(--color-ink);
  font: inherit; font-size: var(--text-input);
  appearance: none;
}
.select {
  padding-right: 36px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23737373' stroke-width='2'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
}
.input:focus, .select:focus { outline: none; border-color: var(--color-hairline); background-color: var(--color-paper); }
input[type=file].input { padding: 10px 14px; font-size: 14px; }
.form-grid { display: grid; gap: 12px; grid-template-columns: 1fr; }

/* 선택 칩 (카드회사·카드 다중 선택) */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 36px; padding: 0 12px;
  border: 0; border-radius: var(--radius-control);
  background: transparent; box-shadow: inset 0 0 0 1px var(--color-hairline);
  color: var(--color-ink); font: inherit; font-size: 13px; cursor: pointer;
}
.chip[aria-pressed=true] { background: var(--color-ink); color: #fafafa; box-shadow: none; }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }

/* 배지 */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px; border-radius: var(--radius-control);
  font-size: var(--text-caption); font-weight: var(--font-weight-medium);
  background: var(--color-canvas); color: var(--color-ink-soft); white-space: nowrap;
}
.badge-solid { background: var(--color-ink-soft); color: #fafafa; }

/* ---------- 조회 조건 ---------- */
.filter { display: grid; gap: 12px; }
.filter-row { display: grid; gap: 8px; grid-template-columns: 1fr 1fr; }
.filter-row.three { grid-template-columns: 1fr 1fr 1fr; }
.filter-toggle { justify-self: start; }
.filter-more { display: grid; gap: 12px; }

/* ---------- 합계 목록 ---------- */
.sum-list { list-style: none; margin: 0; padding: 0; display: grid; }
.sum-item {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 4px 10px;
  min-height: var(--touch-min); padding: 8px 0;
  border-top: 1px solid var(--color-hairline);
}
.sum-item:first-child { border-top: 0; }
.sum-item.clickable { cursor: pointer; }
.sum-item.selected .sum-name { font-weight: var(--font-weight-semibold); text-decoration: underline; text-underline-offset: 3px; }
.sum-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sum-meta { color: var(--color-mid-gray); font-size: var(--text-caption); }
.sum-item.wrap .sum-name { white-space: normal; overflow-wrap: anywhere; }
/* 선택형 합계 행 (대시보드 카드회사별): 체크 표시, 카드 행은 들여쓰기, 해제된 행은 흐리게 */
.check {
  width: 16px; height: 16px; border-radius: 4px; flex: none;
  border: 1.5px solid var(--color-mid-gray); display: grid; place-items: center;
  font-size: 11px; line-height: 1; color: #fafafa;
}
.pick[aria-checked=true] > .check { background: var(--color-ink); border-color: var(--color-ink); }
.pick[aria-checked=true] > .check::after { content: "✓"; }
.pick[aria-checked=mixed] > .check { background: var(--color-ink); border-color: var(--color-ink); }
.pick[aria-checked=mixed] > .check::after { content: "–"; }
.sum-item.sub { padding-left: 20px; border-top-style: dashed; }
.pick[aria-checked=false] .sum-name, .pick[aria-checked=false] .num { color: var(--color-mid-gray); }
.pick[aria-checked=false] .num { text-decoration: line-through; }

/* ---------- 표(그리드) ---------- */
.table-wrap { margin: 0 calc(var(--card-padding) * -1); }
.table { width: 100%; border-collapse: collapse; table-layout: auto; }
.table th, .table td { padding: 0 8px; height: var(--row-height); border-bottom: 1px solid var(--color-hairline); text-align: left; white-space: nowrap; }
.table th:first-child, .table td:first-child { padding-left: var(--card-padding); }
.table th:last-child, .table td:last-child { padding-right: var(--card-padding); }
.table th {
  background: var(--color-surface-alt);
  color: var(--color-mid-gray); font-size: var(--text-caption); font-weight: var(--font-weight-medium);
  height: 40px; white-space: nowrap;
}
.table th.sortable { cursor: pointer; user-select: none; }
.table th .sort { margin-left: 2px; }
.table th.num, .table td.num { text-align: right; }
.table tbody tr.row:hover { background: var(--color-surface-alt); }
/* 거래 행의 카드 색 띠 (값은 --mark 로 넘긴다, PC·모바일 공통) */
.table td.card-mark { box-shadow: inset 3px 0 0 var(--mark); }
.table td.ellipsis { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.table tfoot td { background: var(--color-surface-alt); font-weight: var(--font-weight-semibold); }
/* 모바일: 핵심 열만 표시한다 (업로드 미리보기 표) */
.col-opt { display: none; }
/* 좌우 스크롤 표 (사용내역 거래내역): 모바일에서도 전체 열을 보여주고, 넓으면 표 안에서만 가로 스크롤 */
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.scroll-x .col-opt { display: table-cell; }
.empty { text-align: center; color: var(--color-mid-gray); padding: 32px 0; }
.pager { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 12px; }

/* ---------- 차트 ---------- */
.chart-box { position: relative; height: var(--chart-height); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-bottom: 8px; font-size: var(--text-caption); color: var(--color-ink); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }

/* ---------- 알림 ---------- */
.notice { padding: 12px 14px; border-radius: var(--radius-nested); background: var(--color-canvas); }
.notice.error { color: var(--color-danger); background: var(--color-paper); box-shadow: inset 0 0 0 1px var(--color-hairline); }
.error-list { margin: 8px 0 0; padding-left: 18px; color: var(--color-danger); max-height: 200px; overflow: auto; }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--nav-height) + env(safe-area-inset-bottom) + 16px);
  z-index: 30; max-width: calc(100% - 32px);
  padding: 10px 16px; border-radius: var(--radius-control);
  background: var(--color-ink-soft); color: #fafafa; font-weight: var(--font-weight-medium);
}
.toast.error { background: var(--color-danger); }

/* ---------- 진행 표시 (조회·업로드 중) ---------- */
/* 화면 맨 위 진행 막대: 서버 요청이 0.15초 넘게 걸리면 보인다 (common.js) */
.top-progress {
  position: fixed; top: 0; left: 0; right: 0; z-index: 50;
  height: 3px; overflow: hidden; pointer-events: none;
  opacity: 0; transition: opacity .2s;
}
html.is-busy .top-progress { opacity: 1; }
.top-progress::before {
  content: ""; position: absolute; top: 0; bottom: 0; left: -40%; width: 40%;
  background: var(--color-ink); border-radius: 0 2px 2px 0;
  animation: cp-progress 1.1s ease-in-out infinite;
}
@keyframes cp-progress { from { left: -40%; } to { left: 100%; } }

/* 버튼 안 스피너 */
.spinner {
  width: 16px; height: 16px; flex: none;
  border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%;
  animation: cp-spin .7s linear infinite;
}
@keyframes cp-spin { to { transform: rotate(360deg); } }
.btn[aria-busy=true] { cursor: progress; opacity: 1; }

/* 다시 조회하는 동안 이전 결과를 흐리게 (짧은 요청은 깜빡이지 않도록 늦게 시작) */
.is-loading { opacity: .45; pointer-events: none; transition: opacity .2s .15s; }

@media (prefers-reduced-motion: reduce) {
  .top-progress::before { animation-duration: 3s; }
  .spinner { animation-duration: 2s; }
  .is-loading { transition: none; }
}

/* ---------- 로그인 / 안내 화면 ---------- */
.center-page { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: var(--page-gutter); }
.center-card { width: 100%; max-width: 400px; display: grid; gap: 20px; text-align: center; }
.center-card h1 { font-size: 24px; font-weight: var(--font-weight-semibold); }
.center-card p { margin: 0; color: var(--color-mid-gray); }
.google-btn { width: 100%; }

/* ---------- PC (768px 이상): 배치만 바꾼다 ---------- */
@media (min-width: 768px) {
  .tabs {
    position: static; height: auto; padding: 0; border: 0; background: transparent;
    display: flex; gap: 4px;
  }
  .tab {
    flex-direction: row; gap: 6px;
    height: 36px; padding: 0 14px; border-radius: var(--radius-control);
    font-size: var(--text-body); font-weight: var(--font-weight-medium);
  }
  .tab .icon { display: none; }
  .tab.active { background: var(--color-canvas); }
  .icon-btn .label { display: inline; font-size: 13px; }
  .page { padding-bottom: var(--section-gap); }
  .toast { bottom: 24px; }
  .grid.cols-2 { grid-template-columns: 1fr 1fr; }
  .grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
  .grid .span-2 { grid-column: span 2; }
  .grid .span-all { grid-column: 1 / -1; }
  .form-grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
  .form-grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
  .filter-row, .filter-row.three { grid-template-columns: repeat(4, 1fr); }
  .col-opt { display: table-cell; }
  /* PC에서 열이 많아 폭을 넘으면 표 영역 안에서만 가로 스크롤 (페이지 전체는 스크롤되지 않음) */
  .table-wrap { overflow-x: auto; }
  .table td.ellipsis { max-width: 200px; }
}
