/* ==========================================================================
   통합 손익 대시보드 · 목업 스타일
   라이트 테마 · 고밀도 · 완전 반응형(1280+/1024/768/480)
   ========================================================================== */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
  --text-soft: #9ca3af;
  --navy: #1e3a8a;
  --navy-ink: #eef2ff;
  --good: #16a34a;
  --bad: #dc2626;
  --flat: #6b7280;
  --warn-bg: #fee2e2;
  --radius: 10px;
  --gap: 10px;
  --pad: 11px;
  --font-base: 12px;
  --header-h1: 56px;
  --header-h2: 44px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Pretendard', -apple-system, BlinkMacSystemFont, 'Malgun Gothic', sans-serif;
  font-size: var(--font-base);
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  max-width: 100vw;
  overflow-x: hidden;
}
table { font-variant-numeric: tabular-nums; }
button { font-family: inherit; cursor: pointer; }

/* ---------------------------------------------------------------------- */
/* ① 헤더 */
/* ---------------------------------------------------------------------- */
.app-header {
  position: sticky; top: 0; z-index: 40;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.header-row1 {
  display: flex; align-items: center; gap: 14px;
  padding: 8px 14px;
  min-height: var(--header-h1);
  border-bottom: 1px solid var(--border);
}
.brand-title { display: flex; align-items: center; gap: 8px; }
.brand-title h1 { font-size: 16px; font-weight: 700; margin: 0; white-space: nowrap; }
.live-badge {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 10px; font-weight: 700; color: var(--bad);
  background: #fee2e2; padding: 2px 7px; border-radius: 999px;
}
.live-dot {
  width: 6px; height: 6px; border-radius: 50%; background: var(--bad);
  animation: pulse 1.4s infinite;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(220,38,38,0.55); }
  70% { box-shadow: 0 0 0 6px rgba(220,38,38,0); }
  100% { box-shadow: 0 0 0 0 rgba(220,38,38,0); }
}
.corp-tabs { display: flex; gap: 2px; background: #f1f2f4; border-radius: 8px; padding: 2px; }
.corp-tab {
  border: 0; background: transparent; padding: 5px 12px; border-radius: 6px;
  font-size: 12px; font-weight: 600; color: var(--text-muted);
  min-height: 28px;
}
.corp-tab.is-active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,0.08); }
.header-actions { margin-left: auto; display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.btn-notes {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 6px 12px; border-radius: 7px; font-size: 12px; font-weight: 600;
  min-height: 30px;
}
.btn-notes:hover { background: #f8f9fb; }
.updated-at { font-size: 11px; color: var(--text-muted); }

.header-row2 {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 14px;
  min-height: var(--header-h2);
  overflow: hidden;
}
.row-divider { width: 1px; height: 20px; background: var(--border); flex: 0 0 auto; }
.chip-row-scroll, .period-tabs-scroll {
  overflow-x: auto; scrollbar-width: none; -ms-overflow-style: none;
}
.chip-row-scroll::-webkit-scrollbar, .period-tabs-scroll::-webkit-scrollbar { display: none; }
.chip-row, .period-tabs { display: flex; gap: 5px; flex-wrap: nowrap; }
.chip {
  display: inline-flex; align-items: center; gap: 5px; white-space: nowrap;
  border: 1px solid var(--border); background: var(--surface);
  padding: 4px 10px; border-radius: 999px; font-size: 11.5px; font-weight: 500;
  color: var(--text-muted); min-height: 26px;
}
.chip.is-active { background: #111827; color: #fff; border-color: #111827; }
.chip .color-dot { border-color: rgba(255,255,255,0.6); }
.color-dot { width: 8px; height: 8px; border-radius: 50%; flex: 0 0 auto; border: 1px solid rgba(0,0,0,0.06); }
.period-tabs { flex-wrap: nowrap; }
.period-tab {
  border: 1px solid var(--border); background: var(--surface); white-space: nowrap;
  padding: 4px 11px; border-radius: 7px; font-size: 11.5px; font-weight: 600; color: var(--text-muted);
  min-height: 26px;
}
.period-tab.is-active { background: var(--navy); color: #fff; border-color: var(--navy); }
.compare-label {
  margin-left: auto; font-size: 11px; color: var(--text-muted); white-space: nowrap; flex: 0 0 auto;
  padding-left: 8px;
}

/* ---------------------------------------------------------------------- */
/* 공통 카드 */
/* ---------------------------------------------------------------------- */
.app-main { padding: 10px 14px 20px; display: flex; flex-direction: column; gap: 10px; max-width: 100%; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: var(--pad);
  min-width: 0;
}
.card h2 { font-size: 13px; margin: 0 0 8px; font-weight: 700; }

/* ---------------------------------------------------------------------- */
/* ② KPI 스트립 */
/* ---------------------------------------------------------------------- */
.kpi-strip {
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 8px;
}
.kpi-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 11px; min-width: 0;
}
.kpi-card--highlight { background: var(--navy); border-color: var(--navy); color: #fff; }
.kpi-card--highlight .kpi-label { color: #c7d2fe; }
.kpi-card--highlight .kpi-sub { color: #a5b4fc; }
.kpi-label { font-size: 10.5px; color: var(--text-muted); font-weight: 600; margin-bottom: 4px; }
.kpi-value { font-size: 21px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.15; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-delta { font-size: 11px; font-weight: 600; margin-top: 3px; }
.kpi-sub { font-size: 10.5px; color: var(--text-muted); margin-top: 4px; line-height: 1.35; }
.delta-good { color: var(--good); }
.delta-bad { color: var(--bad); }
.delta-flat { color: var(--flat); }

/* ---------------------------------------------------------------------- */
/* ③ 메인 그리드: 차트 + 채널 패널 */
/* ---------------------------------------------------------------------- */
.main-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 10px; align-items: start; }
.chart-card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.chart-mode-note { font-size: 10.5px; color: var(--text-muted); }
.chart-canvas-wrap { position: relative; height: 320px; margin-top: 6px; }
.channel-panel-card h2 { margin-bottom: 6px; }
.donut-wrap { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
/* Chart.js는 responsive:true + maintainAspectRatio:false일 때 canvas의 "부모 컨테이너" 크기를
   읽어 내부 해상도를 정하는데, 부모가 donut-wrap(flex row, 높이=범례 텍스트 높이)이면 정사각형이
   아닌 크기를 기준으로 그려진 뒤 canvas 자체의 84x84 CSS 강제 크기로 스케일되어 타원으로 찌그러진다.
   → 캔버스를 명시적 정사각형 박스(donut-canvas-box)로 감싸 Chart.js가 그 박스 크기를 읽게 한다. */
.donut-canvas-box { position: relative; width: 84px; height: 84px; flex: 0 0 auto; }
.donut-canvas-box canvas { width: 100% !important; height: 100% !important; }
.donut-legend { display: flex; flex-wrap: wrap; gap: 4px 10px; font-size: 10.5px; color: var(--text-muted); }
.donut-legend-item { display: inline-flex; align-items: center; gap: 4px; }

/* ---------------------------------------------------------------------- */
/* 테이블 공통 */
/* ---------------------------------------------------------------------- */
.table-scroll { overflow-x: auto; width: 100%; }
.table-scroll.scroll-y { max-height: 360px; overflow-y: auto; }
.data-table { border-collapse: collapse; width: 100%; min-width: max-content; font-size: 11.5px; }
.data-table th, .data-table td {
  padding: 6px 8px; text-align: left; white-space: nowrap; border-bottom: 1px solid var(--border);
}
.data-table thead th {
  position: sticky; top: 0; z-index: 2; background: var(--surface);
  font-weight: 700; color: var(--text-muted); font-size: 10.5px; border-bottom: 2px solid var(--border);
}
.data-table th.sortable { cursor: pointer; user-select: none; }
.data-table th.sortable:hover { color: var(--text); }
.sort-arrow { font-size: 9px; }
.data-table td.num, .data-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table td.neg { color: var(--bad); font-weight: 600; }
.data-table td.warn { color: var(--bad); }
.col-sticky {
  position: sticky; left: 0; z-index: 1; background: var(--surface);
  box-shadow: 2px 0 0 0 var(--border);
}
.data-table thead th.col-sticky { z-index: 3; }
.total-row td { font-weight: 700; background: #f8f9fb; }
.total-row td.col-sticky { background: #f8f9fb; }
.row-subtotal td { font-weight: 700; background: var(--navy-ink); color: #3730a3; }
.row-subtotal td.col-sticky { background: var(--navy-ink); }
.row-clickable { cursor: pointer; }
.row-clickable:hover td { background: #f8f9fb; }
.row-clickable:hover td.col-sticky { background: #f1f2f4; }
.row-best td { background: #ecfdf3; }
.row-best td.col-sticky { background: #ecfdf3; }
.row-worst td { background: #fef2f2; }
.row-worst td.col-sticky { background: #fef2f2; }
.row-today td { background: #eef2ff; }
.row-today td.col-sticky { background: #eef2ff; }
.badge-inprogress {
  display: inline-block; margin-left: 5px; font-size: 9.5px; font-weight: 700;
  background: var(--navy); color: #fff; padding: 1px 6px; border-radius: 999px;
}
.dow-label { font-size: 10px; color: var(--text-muted); }
.dow-sat { color: #2563eb; }
.dow-sun { color: var(--bad); }
.corp-badge {
  font-size: 9px; font-weight: 700; padding: 1px 5px; border-radius: 5px;
  background: #f1f2f4; color: var(--text-muted); margin-left: 4px;
}
.corp-badge--parameat { background: #fee2e2; color: #b91c1c; }
.corp-badge--onoffer { background: #e0e7ff; color: #3730a3; }
.corp-badge--fume { background: #fae8ff; color: #a21caf; }
.brand-name-wrap { display: inline-flex; align-items: center; flex-wrap: wrap; gap: 2px; }

.inline-bar {
  position: relative; display: inline-flex; align-items: center;
  width: 64px; height: 12px; background: #f1f2f4; border-radius: 4px; overflow: hidden;
  vertical-align: middle;
}
.inline-bar-fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 4px; opacity: 0.85; }
.inline-bar-text {
  position: relative; z-index: 1; font-size: 9.5px; color: var(--text); font-weight: 600;
  margin-left: auto; margin-right: 3px; text-shadow: 0 0 2px #fff, 0 0 2px #fff;
}
.spark-cell { line-height: 0; }
.sparkline { display: block; }

/* ---------------------------------------------------------------------- */
/* ④ 브랜드 매트릭스 / ⑤ 하단 2분할 */
/* ---------------------------------------------------------------------- */
.bottom-grid { display: grid; grid-template-columns: 1.3fr 1fr; gap: 10px; align-items: start; }

/* ---------------------------------------------------------------------- */
/* ⑥ 개선노트 모달 */
/* ---------------------------------------------------------------------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,0.45);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px;
}
.modal-overlay[hidden] { display: none; }
.modal-panel {
  background: var(--surface); border-radius: 12px; width: min(880px, 100%);
  max-height: 85vh; display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.modal-head h2 { font-size: 14px; margin: 0; }
.modal-close {
  border: 0; background: transparent; font-size: 20px; line-height: 1; color: var(--text-muted);
  width: 32px; height: 32px; border-radius: 8px;
}
.modal-close:hover { background: #f1f2f4; }
.modal-body { padding: 12px 16px; overflow-y: auto; }
.notes-table { width: 100%; border-collapse: collapse; font-size: 11.5px; }
.notes-table th, .notes-table td { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.notes-table th { color: var(--text-muted); font-size: 10.5px; }
.notes-table td.hint { color: var(--text-muted); font-size: 10.5px; font-family: ui-monospace, monospace; }

/* ---------------------------------------------------------------------- */
/* ⑦ 푸터 */
/* ---------------------------------------------------------------------- */
.app-footer {
  text-align: center; font-size: 10.5px; color: var(--text-soft);
  padding: 16px 14px 24px;
}

/* ==========================================================================
   반응형 — 1280+ 데스크톱(기본) / 1024 태블릿 / 768 모바일 / 480 소형 모바일
   ========================================================================== */

/* ---- 태블릿 (~1024px): KPI 8→4x2, 메인차트+채널패널 세로 스택 ---- */
@media (max-width: 1024px) {
  .kpi-strip { grid-template-columns: repeat(4, 1fr); }
  .main-grid { grid-template-columns: 1fr; }
  .chart-canvas-wrap { height: 280px; }
  .bottom-grid { grid-template-columns: 1fr; }
}

/* ---- 모바일 (~768px 이하) ---- */
@media (max-width: 768px) {
  html, body { font-size: 12px; }
  .header-row1 { flex-wrap: wrap; padding: 8px 10px; min-height: auto; gap: 8px; }
  .brand-title h1 { font-size: 14px; }
  .header-actions { margin-left: 0; width: 100%; justify-content: space-between; }
  .header-row2 { padding: 6px 10px; flex-wrap: nowrap; }
  .compare-label { display: none; } /* 공간 확보 — 기간 탭 옆 배지로 대체하지 않고 헤더 밀도 우선 */
  .app-main { padding: 8px 10px 16px; }

  .kpi-strip { grid-template-columns: repeat(2, 1fr); gap: 7px; }
  .kpi-card { padding: 9px 10px; }
  /* 공헌이익 카드도 다른 KPI와 동일 폭 사용 — 전폭(span 2) 강조는 대표 지시로 제거함. 복원 금지 */
  .kpi-value { font-size: 19px; }
  /* 오늘 탭 매출 카드 보조줄("어제 … · 이번달 누적 …")이 2열 카드 폭에서 넘치지 않도록 축소+말줄임 */
  .kpi-card[data-kpi="revenue"] .kpi-sub {
    font-size: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }

  .chart-canvas-wrap { height: 220px; }
  .donut-canvas-box { width: 64px; height: 64px; }

  .chip, .period-tab, .corp-tab, .btn-notes { min-height: 32px; }

  .modal-overlay { padding: 0; }
  .modal-panel { width: 100%; height: 100%; max-height: 100%; border-radius: 0; }
}

/* ---- 소형 모바일 (~480px 이하) ---- */
@media (max-width: 480px) {
  .brand-title { gap: 6px; }
  .live-badge span:not(.live-dot) { display: none; }
  .updated-at { font-size: 10px; }
  .kpi-value { font-size: 17px; }
  .chart-canvas-wrap { height: 200px; }
}
