/* v3/app.css — 다로아 손익 대시보드 v3 셸·컴포넌트 (design_v3 3장)
   규약: 색은 :root 토큰만, important 규칙·인라인 style 없음.
   톤 유틸(.ok/.warn/.danger|.error/.muted)이 --tone/--tone-bg 를 정하고
   kpi 보조·배너·배지·토스트가 그 변수를 읽는다(같은 개념 한 구현). */

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --primary: #6D28D9;                 /* rnd-board 포인트 보라 (hover·선택·활성) */
  --primary-light: #8B5CF6;
  --primary-dark: #1f2a44;            /* rnd-board 네이비 — 주 버튼·헤더 채움 */
  --accent: #c0392b;                  /* rnd-board danger-strong — 배지·위험 채움 */
  --accent-light: #E22E1C;
  --bg-dark: #f0f2f7;                 /* 페이지 배경 */
  --bg-card: #FFFFFF;
  --bg-card-hover: #f8fafc;
  --bg-input: #f8fafc;
  --text-white: #222;                 /* 본문 잉크 */
  --text-light: #3a4557;
  --text-muted: #636F81;              /* WCAG 4.5+ */
  --border: #dde3ec;
  --success: #1e7d45;
  --danger: #c0392b;
  --warning: #7a5f14;
  --shadow: 0 2px 10px rgba(31,42,68,.06);
  --radius: 10px;
  --radius-sm: 6px;
  /* v3 추가 토큰: 기존 CSS의 리터럴 색을 이름으로 옮긴 것 */
  --header-bg: var(--primary-dark);
  --header-fg: #FFFFFF;
  --header-sub: #aeb8cc;
  --header-hover: rgba(255,255,255,.10);
  --on-primary: #FFFFFF;
  --focus: #7096CD;
  --th-bg: #eef3fa;
  --th-fg: #2a4e79;
  --th-line: #dde6f1;
  --hover-bg: rgba(31,42,68,.04);
  --ok-bg: rgba(30,125,69,.10);
  --warn-bg: rgba(138,109,31,.12);
  --danger-bg: rgba(192,57,43,.10);
  --ok-on-dark: #7EE2A8;
  --warn-on-dark: #F5C451;
  --danger-on-dark: #FF8A80;
  --backdrop: rgba(20,26,42,.45);
  --shadow-lg: 0 12px 32px rgba(31,42,68,.18);
}

body {
  font-family: 'Pretendard', '맑은 고딕', -apple-system, sans-serif;
  background: var(--bg-dark);
  color: var(--text-white);
  min-height: 100vh;
  line-height: 1.6;
  overflow-x: hidden;
}
button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
a { color: var(--primary); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
.header :focus-visible { outline-color: var(--header-fg); }
.num, .kpi-value, .kpi-sub, tbody td, tfoot td { font-variant-numeric: tabular-nums; }

/* ---------- 톤 유틸 (컴포넌트 공통) ---------- */
.ok { --tone: var(--success); --tone-bg: var(--ok-bg); }
.warn { --tone: var(--warning); --tone-bg: var(--warn-bg); }
.danger, .error { --tone: var(--danger); --tone-bg: var(--danger-bg); }
.muted { --tone: var(--text-muted); --tone-bg: var(--bg-input); color: var(--text-muted); }

/* ---------- 헤더 1줄: 제목 | 탭 | 기간 | (topbar 슬롯) | 상태 ---------- */
.header {
  display: flex; align-items: center; flex-wrap: wrap; gap: 8px 14px;
  padding: 8px 16px;
  background: var(--header-bg); color: var(--header-fg);
  box-shadow: var(--shadow);
  position: sticky; top: var(--topbar-h, 0px); z-index: 100;
}
body:has(#bf-auth-bar) { --topbar-h: 36px; } /* auth.js 고정 상단바(단독·PWA 경로)만큼 내림. 허브 iframe 에는 바가 없어 0 */
.header-title { display: flex; align-items: center; gap: 10px; min-width: 0; }
.header-title h1 { font-size: 1.05rem; font-weight: 800; letter-spacing: -.3px; white-space: nowrap; }
.badge {
  background: var(--accent); color: var(--header-fg);
  font-size: .7rem; font-weight: 800; padding: 3px 10px; border-radius: 999px; white-space: nowrap;
}
#bfTools { display: inline-flex; align-items: center; gap: 8px; margin-left: auto; }
.tabs { display: flex; gap: 2px; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.tab-btn {
  padding: 7px 14px; border: 0; border-radius: var(--radius-sm);
  background: transparent; color: var(--header-sub);
  font-size: .9rem; font-weight: 700; cursor: pointer; white-space: nowrap;
  transition: background .15s, color .15s;
}
.tab-btn:hover { color: var(--header-fg); background: var(--header-hover); }
.tab-btn.active { color: var(--on-primary); background: var(--primary); }
.tab-btn:focus-visible { outline-offset: -2px; } /* .tabs 가 overflow-x:auto 스크롤 컨테이너라 바깥쪽 링이 잘림 → 링을 버튼 안쪽에 그림 */
.period { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.nav-btn {
  width: 34px; height: 34px; border: 1px solid var(--header-hover); border-radius: var(--radius-sm);
  background: var(--header-hover); color: var(--header-fg); cursor: pointer; font-size: .8rem; line-height: 1;
}
.nav-btn:hover { background: var(--primary); }
.header-status { flex: 1 1 240px; min-width: 0; }
.header-status .banner {
  margin: 0; padding: 5px 10px; font-size: .78rem; line-height: 1.4;
  background: var(--header-hover); border-color: transparent; border-left-color: var(--tone);
  color: var(--header-fg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: block; cursor: pointer;
}
.header-status .ok { --tone: var(--ok-on-dark); }
.header-status .warn { --tone: var(--warn-on-dark); }
.header-status .error, .header-status .danger { --tone: var(--danger-on-dark); }

/* ---------- 본문 레이아웃 ---------- */
.main { width: 100%; max-width: 1200px; margin: 0 auto; padding: 20px 16px; min-width: 0; }
.view { min-width: 0; }
.kpi-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; margin-bottom: 18px; }
.kpi-grid--3 { grid-template-columns: repeat(3, minmax(0, 1fr)); } /* 배달 6장 = 3×2 */
.kpi-grid--5 { grid-template-columns: repeat(5, minmax(0, 1fr)); } /* 제품 카테고리 5장 */
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.stack { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.section-note { font-size: .82rem; color: var(--text-light); margin-bottom: 12px; overflow-wrap: anywhere; }
.note { font-size: .8rem; color: var(--text-muted); }
.chart-box { position: relative; height: 280px; min-width: 0; }
.chart-box canvas { max-width: 100%; } /* 뷰포트 축소 시 Chart.js 가 다시 재는 동안 문서 폭 넘침 방지 */
.chart-box--donut { max-width: 320px; margin: 0 auto; }
.table-slot { min-width: 0; }
.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; }
.filters__item { display: flex; align-items: center; gap: 6px; font-size: .82rem; color: var(--text-light); }
.filters__count { margin-left: auto; font-size: .8rem; }

/* ---------- 폼 컨트롤 ---------- */
input[type="date"], input[type="month"], input[type="text"], input[type="password"], select, .select {
  height: 36px; padding: 0 10px;
  background: var(--bg-input); border: 1px solid var(--border); border-radius: var(--radius-sm);
  color: var(--text-white); font-size: .88rem; outline: none; transition: border-color .15s;
}
input:focus, select:focus { border-color: var(--focus); }
input[type="date"]::-webkit-calendar-picker-indicator, input[type="month"]::-webkit-calendar-picker-indicator { cursor: pointer; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--bg-card); color: var(--text-light);
  font-size: .85rem; font-weight: 700; cursor: pointer; white-space: nowrap; transition: background .15s, color .15s;
}
.btn:hover { background: var(--bg-card-hover); color: var(--text-white); }
.btn-primary { background: var(--primary-dark); border-color: var(--primary-dark); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn-outline { background: transparent; border-color: var(--primary); color: var(--primary); }
.btn-danger { background: var(--accent); border-color: var(--accent); color: var(--on-primary); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- 세그먼트(일/주/월) ---------- */
.segment {
  display: inline-flex; gap: 2px; padding: 3px;
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.segment button {
  min-width: 44px; padding: 4px 12px; border: 0; border-radius: 4px;
  background: transparent; color: var(--text-light); font-size: .85rem; font-weight: 600; cursor: pointer;
}
.segment button:hover { background: var(--hover-bg); color: var(--text-white); }
.segment button.active { background: var(--primary); color: var(--on-primary); }

/* ---------- KPI 카드 ---------- */
.kpi-card {
  position: relative; min-width: 0;
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; transition: transform .2s, box-shadow .2s;
}
.kpi-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.kpi-label { font-size: .78rem; font-weight: 600; color: var(--text-muted); margin-bottom: 6px; padding-right: 48px; }
.kpi-value { font-size: 1.6rem; font-weight: 800; line-height: 1.2; color: var(--text-white); overflow-wrap: anywhere; }
.kpi-sub { font-size: .78rem; font-weight: 600; margin-top: 6px; color: var(--tone, var(--text-muted)); }
.kpi-badge {
  position: absolute; top: 10px; right: 10px;
  font-size: .68rem; font-weight: 700; padding: 2px 8px; border-radius: 999px;
  color: var(--tone, var(--text-muted)); background: var(--tone-bg, var(--bg-input));
}

/* ---------- 카드 · 접힘(fold) ---------- */
.card {
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; margin-bottom: 16px; min-width: 0;
}
.card-head, details.fold > summary {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  margin-bottom: 14px;
}
.card-title { font-size: .95rem; font-weight: 700; color: var(--text-white); min-width: 0; }
.card-right { font-size: .8rem; color: var(--text-muted); margin-left: auto; text-align: right; }
.card-body { min-width: 0; }
details.fold > summary { list-style: none; cursor: pointer; user-select: none; margin-bottom: 0; }
details.fold > summary::-webkit-details-marker { display: none; }
details.fold > summary::after { content: '펼치기'; font-size: .8rem; font-weight: 600; color: var(--primary); white-space: nowrap; }
details.fold[open] > summary { margin-bottom: 14px; }
details.fold[open] > summary::after { content: '접기'; }

/* ---------- 표 ---------- */
.table-wrap {
  overflow: auto; max-height: 480px; min-width: 0;
  border: 1px solid var(--border); border-radius: var(--radius-sm); overscroll-behavior: contain;
}
table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: .85rem; }
thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--th-bg); color: var(--th-fg); font-weight: 800;
  padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--th-line); white-space: nowrap;
}
tbody td, tfoot td { padding: 9px 12px; border-bottom: 1px solid var(--border); color: var(--text-light); vertical-align: top; }
tbody tr:hover { background: var(--hover-bg); }
tfoot td, tr.total td { position: sticky; bottom: 0; font-weight: 800; color: var(--text-white); background: var(--th-bg); border-top: 1px solid var(--th-line); }
/* 금액 + 비율 2줄 셀(배달 표): 금액은 안 끊기고 비율은 둘째 줄 */
.table-wrap .amt { white-space: nowrap; }
.table-wrap .ratio { display: block; font-size: .8em; opacity: .75; white-space: nowrap; }
.table-wrap .amt--neg { color: var(--danger); }          /* 비용·손실: 빨간 − (합계 행도 밝은 배경이라 동일) */
.table-wrap .amt--pos { color: var(--success); }         /* 이익: 초록 + */
/* 첫 열 고정(가로 스크롤 시 플랫폼·날짜가 남아 있게) */
.table-wrap--sticky th:first-child, .table-wrap--sticky td:first-child { position: sticky; left: 0; z-index: 2; background: var(--bg-card); white-space: nowrap; }
.table-wrap--sticky thead th:first-child { z-index: 3; background: var(--th-bg); }
.table-wrap--sticky tfoot td:first-child, .table-wrap--sticky tr.total td:first-child { z-index: 3; background: var(--th-bg); }
.table-wrap--sticky tbody tr:hover td:first-child { background: var(--bg-card-hover); }
.num, .text-right, th.right, td.right { text-align: right; }
.center, .text-center { text-align: center; }

/* ---------- 상태 배너 · 빈 상태 · 스켈레톤 ---------- */
.banner {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px; margin-bottom: 14px;
  border: 1px solid var(--border); border-left: 4px solid var(--tone, var(--text-muted)); border-radius: 8px;
  background: var(--tone-bg, var(--bg-card)); color: var(--text-light);
  font-size: .82rem; line-height: 1.5; overflow-wrap: anywhere;
}
.empty {
  padding: 28px 12px; text-align: center; color: var(--text-muted); font-size: .88rem;
  border: 1px dashed var(--border); border-radius: var(--radius-sm);
}
.skeleton { animation: pulse 1.2s ease-in-out infinite; pointer-events: none; }
@keyframes pulse { 0%, 100% { opacity: .55; } 50% { opacity: 1; } }

/* ---------- 토스트 ---------- */
.toast-container {
  position: fixed; top: calc(var(--topbar-h, 0px) + 16px); right: 16px; z-index: 9999;
  display: flex; flex-direction: column; gap: 10px; max-width: calc(100vw - 32px);
}
.toast {
  background: var(--bg-card); border: 1px solid var(--border); border-left: 4px solid var(--tone, var(--success));
  padding: 12px 18px; border-radius: var(--radius-sm); box-shadow: var(--shadow);
  font-size: .85rem; color: var(--text-white); max-width: 360px; animation: toast-in .3s ease;
}
@keyframes toast-in { from { transform: translateX(100%); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- 모달 (prompt/confirm 대체 1종) ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 1000; padding: 16px;
  background: var(--backdrop); display: flex; align-items: center; justify-content: center;
}
.modal {
  width: min(480px, 100%); max-height: 90vh; overflow: auto; padding: 20px;
  background: var(--bg-card); border-radius: var(--radius); box-shadow: var(--shadow-lg); color: var(--text-white);
}
.modal-title { font-size: 1rem; font-weight: 800; margin-bottom: 12px; }
.modal-body { font-size: .88rem; color: var(--text-light); line-height: 1.6; overflow-wrap: anywhere; }
.modal-body ul { padding-left: 18px; margin-top: 8px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; flex-wrap: wrap; }
html.modal-open { overflow: hidden; }

/* ---------- 관리 탭 (views/admin.js 전용, 토큰만; 톤 유틸 .ok/.warn/.danger 가 --tone 을 정한다) ---------- */
.adm-stack { display: flex; flex-direction: column; gap: 14px; }
.adm-stack h3 { font-size: .85rem; font-weight: 700; margin: 4px 0 0; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.adm-stack h3 small { font-size: .75rem; font-weight: 500; color: var(--warning); }
.adm-kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; font-size: .82rem; margin: 0; }
.adm-kv dt { color: var(--text-muted); font-weight: 500; }
.adm-kv dd { margin: 0; word-break: break-all; }
.adm-warn-list { margin: 0; padding-left: 18px; font-size: .82rem; color: var(--warning); }
.adm-pill { display: flex; align-items: center; gap: 8px; font-size: .85rem; line-height: 1.7; flex-wrap: wrap; }
.adm-pill i { width: 10px; height: 10px; border-radius: 50%; background: var(--tone, var(--text-muted)); flex: none; }
.adm-code { background: var(--bg-input); padding: 2px 6px; border-radius: 3px; font-size: .8rem; }
.adm-form { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.adm-form input[type="password"] { min-width: 250px; }
.adm-status { font-size: .82rem; color: var(--tone, var(--text-muted)); flex-basis: 100%; }
.adm-detail summary { cursor: pointer; color: var(--primary); font-weight: 600; }
.adm-detail-body { min-width: 300px; white-space: normal; line-height: 1.55; margin-top: 8px; }
.adm-detail-item { margin-bottom: 6px; }
.adm-detail-options { margin-left: 12px; color: var(--text-muted); }
.adm-detail-refund { margin-top: 6px; color: var(--warning); }
.adm-detail-refund.muted { color: var(--text-muted); }
.adm-detail-sub { margin-top: 8px; }
.adm-detail-sub summary { cursor: pointer; color: var(--text-light); font-weight: 500; }
.adm-detail-raw { margin-top: 6px; }
.adm-links { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.adm-links a { color: var(--primary); font-weight: 700; }
.adm-links span { display: block; font-size: .8rem; color: var(--text-muted); }
.adm-skeleton { height: 96px; border-radius: var(--radius-sm); background: var(--bg-input); }

/* ---------- 반응형 3단계: ≥1025 4열 / 641~1024 2열 / ≤640 1열 ---------- */
@media (max-width: 1024px) {
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
  .header-status { flex-basis: 100%; }
}
@media (max-width: 640px) {
  .header { padding: 8px 12px; gap: 8px 10px; }
  /* 폰: 음수 top 으로 제목·bfTools 행은 스크롤로 밀려나고 탭·기간·상태 행만 고정 (8+27+8+30=73) */
  .header { top: -73px; }
  body:has(#bf-auth-bar) .header { top: -37px; } /* 단독·PWA 경로: auth.js 상단바 36px 만큼 보정 */
  .header-title { flex: 1 1 auto; }
  #bfTools { flex-wrap: wrap; max-width: 100%; justify-content: flex-end; }
  .tabs { order: 3; flex-basis: 100%; }
  .tab-btn { flex: 1 1 auto; text-align: center; padding: 7px 8px; font-size: .84rem; }
  .period { order: 4; flex-basis: 100%; flex-wrap: nowrap; }
  .period input { flex: 1 1 110px; min-width: 0; padding: 0 6px; } /* 세그먼트+◀▶+입력이 한 줄에 들어가게 */
  .segment button { min-width: 36px; padding: 4px 8px; }
  .header-status { order: 5; }
  .main { padding: 14px 12px; }
  .kpi-grid { grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .kpi-card, .card { padding: 14px; }
  .kpi-value { font-size: 1.3rem; }
  .chart-box { height: 240px; }
  .filters__count { margin-left: 0; flex-basis: 100%; }
}
