/* Statistics Module Styles - 히트맵 범례 수정 */

/* ===== 변수 정의 ===== */
#statistics-view {
  /* 통계 전용 색상 */
  --stats-bg: #f8fafc;
  --stats-card-bg: white;
  --stats-border: #e2e8f0;
  --stats-text-primary: #1e293b;
  --stats-text-secondary: #64748b;
  
  /* 차트 색상 */
  --chart-primary: #0ea5e9;
  --chart-secondary: #0369a1;
  --chart-accent: #7dd3fc;
  --chart-light: #e0f2fe;
  --chart-dark: #0c4a6e;
  
  /* 히트맵 색상 */
  --heatmap-level-0: #ebedf0;
  --heatmap-level-1: #9be9a8;
  --heatmap-level-2: #40c463;
  --heatmap-level-3: #30a14e;
  --heatmap-level-4: #216e39;
}

/* Dark mode overrides - 확실한 우선순위 보장 */
html[data-theme="dark"] #statistics-view {
  --stats-bg: var(--dm-background);
  --stats-card-bg: var(--dm-surface);
  --stats-border: var(--dm-border);
  --stats-text-primary: var(--dm-text-primary);
  --stats-text-secondary: var(--dm-text-secondary);
  --chart-light: rgba(14, 165, 233, 0.1);
}

/* Force dark mode styles directly on elements to bypass variable issues */
html[data-theme="dark"] #statistics-view .stat-card,
html[data-theme="dark"] #statistics-view .chart-container,
html[data-theme="dark"] #statistics-view .stats-select,
html[data-theme="dark"] #statistics-view .stats-empty,
html[data-theme="dark"] #statistics-view .stats-error {
  background-color: var(--dm-surface) !important;
  border-color: var(--dm-border) !important;
  color: var(--dm-text-primary) !important;
}

html[data-theme="dark"] #statistics-view .stat-card h3,
html[data-theme="dark"] #statistics-view .stat-content h3,
html[data-theme="dark"] #statistics-view .chart-container h3,
html[data-theme="dark"] #statistics-view .stats-header h2 {
  color: var(--dm-text-primary) !important;
}

html[data-theme="dark"] #statistics-view .stat-content p,
html[data-theme="dark"] #statistics-view .stat-content small,
html[data-theme="dark"] #statistics-view .chart-description {
  color: var(--dm-text-secondary) !important;
}

html[data-theme="dark"] #statistics-view .heatmap-wrapper::-webkit-scrollbar {
  background: var(--dm-surface) !important;
}

/* ===== 시스템 다크 모드 지원 (prefers-color-scheme) ===== */
/* 사용자가 '시스템' 테마를 선택했을 때 data-theme 속성이 없으므로 미디어 쿼리 필요 */
@media (prefers-color-scheme: dark) {
  /* data-theme 속성이 없을 때만 적용 (명시적 테마 설정이 우선) */
  html:not([data-theme]) #statistics-view {
    --stats-bg: #111827;
    --stats-card-bg: #1f2937;
    --stats-border: #374151;
    --stats-text-primary: #f9fafb;
    --stats-text-secondary: #d1d5db;
    --chart-light: rgba(14, 165, 233, 0.1);
  }

  html:not([data-theme]) #statistics-view .stat-card,
  html:not([data-theme]) #statistics-view .chart-container,
  html:not([data-theme]) #statistics-view .stats-select,
  html:not([data-theme]) #statistics-view .stats-empty,
  html:not([data-theme]) #statistics-view .stats-error {
    background-color: #1f2937 !important;
    border-color: #374151 !important;
    color: #f9fafb !important;
  }

  html:not([data-theme]) #statistics-view .stat-card h3,
  html:not([data-theme]) #statistics-view .stat-content h3,
  html:not([data-theme]) #statistics-view .chart-container h3,
  html:not([data-theme]) #statistics-view .stats-header h2,
  html:not([data-theme]) #statistics-view h2,
  html:not([data-theme]) #statistics-view h3 {
    color: #f9fafb !important;
  }

  html:not([data-theme]) #statistics-view .stat-content p,
  html:not([data-theme]) #statistics-view .stat-content small,
  html:not([data-theme]) #statistics-view .chart-description,
  html:not([data-theme]) #statistics-view p,
  html:not([data-theme]) #statistics-view small {
    color: #d1d5db !important;
  }

  html:not([data-theme]) #statistics-view .heatmap-wrapper::-webkit-scrollbar {
    background: #1f2937 !important;
  }
  
  html:not([data-theme]) #statistics-view .heatmap-wrapper::-webkit-scrollbar-thumb {
    background: #4b5563 !important;
  }
}


/* ===== 통계 카드 ===== */
.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1.5rem;
  margin-bottom: 3rem;
}

#statistics-view .stat-card {
  background: var(--stats-card-bg) !important;
  border-radius: 12px;
  padding: 1.5rem;
  border: 1px solid var(--stats-border);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  gap: 1rem;
}

#statistics-view .stat-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(to right, var(--chart-primary), var(--chart-secondary));
  opacity: 0;
  transition: opacity 0.3s ease;
}

#statistics-view .stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

#statistics-view .stat-card:hover::before {
  opacity: 1;
}

/* 아이콘 스타일 */
#statistics-view .stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  flex-shrink: 0;
  background: var(--chart-light);
  color: var(--chart-primary);
}

/* 카드별 아이콘 색상 */
#statistics-view .stat-card:nth-child(1) .stat-icon {
  background: #fee2e2;
  color: #dc2626;
}

#statistics-view .stat-card:nth-child(2) .stat-icon {
  background: #dbeafe;
  color: #2563eb;
}

#statistics-view .stat-card:nth-child(3) .stat-icon {
  background: #fef3c7;
  color: #d97706;
}

#statistics-view .stat-card:nth-child(4) .stat-icon {
  background: #d1fae5;
  color: #059669;
}

/* 콘텐츠 스타일 */
/* 콘텐츠 스타일 */
#statistics-view .stat-content {
  flex: 1;
}

#statistics-view .stat-content h3 {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--stats-text-primary);
  margin: 0;
  line-height: 1.2;
}

#statistics-view .stat-content h3 small {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--stats-text-secondary);
  margin-left: 0.25rem;
}

#statistics-view .stat-content p {
  font-size: 0.875rem;
  color: var(--stats-text-secondary);
  margin: 0.25rem 0 0 0;
}

/* ===== 통계 컨트롤 ===== */
.stats-controls {
  margin-bottom: 2rem;
  display: flex;
  gap: 1rem;
  align-items: center;
  flex-wrap: wrap;
}

#statistics-view .stats-select {
  padding: 0.5rem 1rem;
  border: 1px solid var(--stats-border);
  border-radius: 8px;
  background: var(--stats-card-bg) !important;
  font-size: 0.875rem;
  color: var(--stats-text-primary);
  cursor: pointer;
  transition: all 0.2s ease;
}

#statistics-view .stats-select:hover {
  border-color: var(--chart-primary);
}

#statistics-view .stats-select:focus {
  outline: none;
  border-color: var(--chart-primary);
  box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.1);
}

/* ===== 차트 그리드 ===== */
.statistics-view-active .stats-grid,
#statistics-view.active .stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  gap: clamp(12px, 2vw, 24px);
  margin-bottom: 1.5rem;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  align-items: stretch;   /* 같은 행 카드 높이 일치 */
  grid-auto-rows: 1fr;    /* 행 높이를 균등 분배 */
}

/* ===== 차트 컨테이너 ===== */
#statistics-view .chart-container {
  background: var(--stats-card-bg) !important;
  border-radius: 12px;
  padding: 1rem;
  border: 1px solid var(--stats-border);
  /* min-height: 400px; */
  position: relative;
  min-width: 350px;
  /* height: 600px !important; */
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  overflow: visible;
}

#statistics-view .chart-container.daily-trend-container {
  position: relative;
  overflow: hidden;
}

/* 차트 래퍼가 남은 세로 공간을 채우도록 */
#statistics-view .chart-container .chart-wrapper {
  flex: 1 1 auto;
  width: 100%;
}

#statistics-view .chart-container h3 {
  margin: 0 0 0.5rem 0;
  font-size: 1.125rem;
  color: var(--stats-text-primary);
}

#statistics-view .chart-description {
  font-size: 0.875rem;
  color: var(--stats-text-secondary);
  margin-bottom: 0.5rem;
}

/* Canvas 스타일 */
#statistics-view .chart-container canvas {
  display: block;
  max-width: 100%;
  /* height는 JS에서 지정 */
}

/* ===== 히트맵 스타일 ===== */
#statistics-view .heatmap-container {
  padding: 0.5rem 0;   /* 상하만 최소 패딩 */
  overflow: hidden;
  position: relative;
}

/* 히트맵 목표 필터 칩 */
#statistics-view .heatmap-goal-filter {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
  padding: 4px 0;
}

#statistics-view .goal-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 16px;
  border: 1px solid var(--stats-border);
  background: var(--stats-card-bg);
  color: var(--stats-text-secondary);
  font-size: 0.813rem;
  cursor: pointer;
  transition: all 0.2s ease;
  max-width: 150px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#statistics-view .goal-chip:hover {
  border-color: var(--chart-primary);
  color: var(--chart-primary);
}

#statistics-view .goal-chip.active {
  background: var(--chart-primary);
  border-color: var(--chart-primary);
  color: white;
}

#statistics-view .goal-chip-more {
  background: transparent;
  border-style: dashed;
}

#statistics-view .goal-chip-more .more-count {
  font-size: 0.75rem;
  opacity: 0.7;
}

/* 더 보기 모달 */
#statistics-view .goal-filter-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  transition: all 0.2s ease;
}

#statistics-view .goal-filter-modal.show {
  opacity: 1;
  visibility: visible;
}

#statistics-view .goal-filter-modal-content {
  background: var(--stats-card-bg);
  border-radius: 12px;
  padding: 20px;
  max-width: 400px;
  width: 90%;
  max-height: 70vh;
  overflow-y: auto;
}

#statistics-view .goal-filter-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--stats-border);
}

#statistics-view .goal-filter-modal-header h4 {
  margin: 0;
  color: var(--stats-text-primary);
}

#statistics-view .goal-filter-modal-close {
  background: none;
  border: none;
  font-size: 1.25rem;
  cursor: pointer;
  color: var(--stats-text-secondary);
}

#statistics-view .goal-filter-section {
  margin-bottom: 16px;
}

#statistics-view .goal-filter-section-title {
  font-size: 0.75rem;
  color: var(--stats-text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

#statistics-view .goal-filter-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#statistics-view .goal-filter-item {
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--stats-bg);
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

#statistics-view .goal-filter-item:hover {
  background: var(--chart-light);
}

#statistics-view .goal-filter-item.selected {
  background: var(--chart-primary);
  color: white;
}

#statistics-view .goal-filter-item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.875rem;
}

#statistics-view .goal-filter-item-status {
  font-size: 0.75rem;
  opacity: 0.7;
}

/* Dark mode */
html[data-theme="dark"] #statistics-view .goal-chip {
  background: var(--dm-surface);
  border-color: var(--dm-border);
  color: var(--dm-text-secondary);
}

html[data-theme="dark"] #statistics-view .goal-chip.active {
  background: var(--chart-primary);
  border-color: var(--chart-primary);
  color: white;
}

html[data-theme="dark"] #statistics-view .goal-filter-modal-content {
  background: var(--dm-surface);
}

html[data-theme="dark"] #statistics-view .goal-filter-item {
  background: var(--dm-surface-hover);
}

html[data-theme="dark"] #statistics-view .goal-filter-item:hover {
  background: rgba(14, 165, 233, 0.2);
}

#statistics-view .heatmap-wrapper {
  min-height: 0;
  overflow-x: auto; 
  overflow-y: hidden;
  scrollbar-color: var(--stats-text-secondary) var(--stats-card-bg); /* Firefox */
}

/* Webkit browsers (Chrome, Safari, Edge) */
#statistics-view .heatmap-wrapper::-webkit-scrollbar {
  height: 8px;
  background: var(--stats-card-bg);
}

#statistics-view .heatmap-wrapper::-webkit-scrollbar-thumb {
  background: var(--stats-text-secondary);
  border-radius: 4px;
}

#statistics-view .heatmap-wrapper::-webkit-scrollbar-thumb:hover {
  background: var(--stats-text-primary);
}

#statistics-view .heatmap-grid { width:100%; }

#statistics-view .heatmap-cell {
  aspect-ratio: 1 / 1;
  background: var(--heatmap-level-0);
  border-radius: 2px;
  cursor: pointer;
  transition: all 0.2s ease;
}

#statistics-view .heatmap-cell:hover {
  transform: scale(1.3);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  z-index: 10;
}

/* 히트맵 레벨 */
#statistics-view .heatmap-cell[data-level="1"] { background: var(--heatmap-level-1); }
#statistics-view .heatmap-cell[data-level="2"] { background: var(--heatmap-level-2); }
#statistics-view .heatmap-cell[data-level="3"] { background: var(--heatmap-level-3); }
#statistics-view .heatmap-cell[data-level="4"] { background: var(--heatmap-level-4); }

/* 히트맵 툴팁 */
#statistics-view .heatmap-tooltip {
  position: absolute;
  background: rgba(0, 0, 0, 0.9);
  color: white;
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  font-size: 0.75rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease;
  transform: translate(-50%, -100%);
  margin-top: -8px;
  z-index: 1000;
}

#statistics-view .heatmap-tooltip.show {
  opacity: 1;
}

#statistics-view .heatmap-tooltip::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid rgba(0, 0, 0, 0.9);
}

/* ===== 히트맵 범례 (수정됨) ===== */
#statistics-view .heatmap-legend { display:flex; gap:8px; align-items:center; justify-content:flex-end; margin-top:6px; font-size:0.75rem; color: var(--stats-text-secondary); }

/* 범례 항목 컨테이너 */
#statistics-view .heatmap-legend > div {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

/* 범례의 색상 박스 */
#statistics-view .heatmap-legend .heatmap-cell {
  display: inline-block;
  width: 12px !important;
  height: 12px !important;
  border-radius: 2px;
  cursor: default;
  flex-shrink: 0;
}

/* 범례 텍스트 */
#statistics-view .heatmap-legend small {
  font-size: 0.75rem;
  color: var(--stats-text-secondary);
  white-space: nowrap;
}

/* ===== 통계 헤더 ===== */
#statistics-view .stats-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--stats-border);
}

#statistics-view .stats-header h2 {
  margin: 0;
  color: var(--stats-text-primary);
}

/* ===== 빈 상태 & 에러 ===== */
#statistics-view .stats-empty,
#statistics-view .stats-error {
  text-align: center;
  padding: 3rem;
  color: var(--stats-text-secondary);
  background: var(--stats-bg);
  border-radius: 12px;
  border: 1px dashed var(--stats-border);
}

#statistics-view .stats-empty h4,
#statistics-view .stats-error h4 {
  margin: 0 0 0.5rem 0;
  color: var(--stats-text-primary);
}

/* ===== 반응형 디자인 ===== */
@media (max-width: 1024px) {
  .stats-grid {
    grid-template-columns: 1fr;
    min-width: auto;
  }
  
  .chart-container {
    min-width: auto;
  }
}

/* === Statistics layout hotfix overrides === */
#statistics-view.active .stats-grid,
.statistics-view-active .stats-grid {
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)) !important;
  /* grid-auto-rows 제거 */
  gap: clamp(12px, 2vw, 24px) !important;
  min-width: 0 !important;
  align-items: start !important; /* Changed from stretch to prevent unwanted growing */
}

#statistics-view.active .chart-container,
.statistics-view-active .chart-container {
  height: auto !important;
  min-height: 450px !important; /* 높이 상향 조정 */
  padding: 1rem !important;
  margin-bottom: 0 !important; /* 그리드 gap으로 제어되므로 0으로 변경 */
  display: flex; /* 자식 요소의 유연한 크기 조정을 위해 flexbox 사용 */
  flex-direction: column;
}

/* Heatmap container should not have a large min-height */
#statistics-view.active .heatmap-container,
.statistics-view-active .heatmap-container {
  min-height: auto !important;
}

#statistics-view.active .daily-trend-container,
.statistics-view-active .daily-trend-container {
  padding: 1rem !important;
}

#time-per-task-chart {
  /* height: 300px !important; */ /* 고정 높이 제거 */
  max-height: none !important;
}

#statistics-view.active .heatmap-container,
.statistics-view-active .heatmap-container {
  padding: 8px 12px !important;
  overflow-x: auto !important;
  overflow-y: hidden !important;
}

@media (max-width: 768px) {
  #statistics-view.active .stats-grid,
  .statistics-view-active .stats-grid {
    grid-template-rows: auto !important;
  }
  #statistics-view.active .chart-container,
  .statistics-view-active .chart-container {
    height: auto !important;
  }
}

@media (max-width: 768px) {
  /* 모바일: 통계 타이틀과 셀렉트 바 한 줄 유지 */
  .statistics-view-active .view-header,
  #statistics-view.active .view-header {
    flex-direction: row !important;
    align-items: center !important;
    justify-content: space-between !important;
    gap: 0.5rem !important;
    flex-wrap: nowrap !important;
  }
  .statistics-view-active .view-header h2,
  #statistics-view.active .view-header h2 {
    font-size: 1.125rem !important;
    margin: 0;
    white-space: nowrap;
  }
  .statistics-view-active .stats-controls,
  #statistics-view.active .stats-controls {
    flex: 0 0 auto;
    gap: 0.5rem !important;
  }
  .statistics-view-active .stats-select,
  #statistics-view.active .stats-select {
    font-size: 0.85rem;
    padding: 0.4rem 0.6rem;
  }
  /* KPI 3개: 모바일에서도 항상 한 줄(3칸) + 아이콘 제거 + 가운데 정렬 */
  .statistics-view-active .stats-cards,
  #statistics-view.active .stats-cards {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    gap: 0.4rem !important;
  }
  .statistics-view-active .stat-card,
  #statistics-view.active .stat-card {
    padding: 0.5rem !important;
    gap: 0.25rem !important;
    justify-content: center !important;
  }
  .statistics-view-active .stat-icon,
  #statistics-view.active .stat-icon {
    display: none !important;
  }
  .statistics-view-active .stat-content,
  #statistics-view.active .stat-content {
    text-align: center !important;
    margin: 0 auto !important;
  }
  .statistics-view-active .stat-content h3,
  #statistics-view.active .stat-content h3 {
    font-size: 0.9rem !important;
    white-space: nowrap !important;
  }
  .statistics-view-active .stat-content h3 small,
  #statistics-view.active .stat-content h3 small {
    font-size: 0.65rem !important;
  }
  .statistics-view-active .stat-content p,
  #statistics-view.active .stat-content p {
    font-size: 0.7rem !important;
  }
  
  .heatmap-grid {
    gap: 2px;
  }
  
  .heatmap-cell {
    width: 10px;
    height: 10px;
  }
  
  /* 모바일에서 범례 크기 조정 */
  .heatmap-legend {
    font-size: 0.7rem;
  }
  
  .heatmap-legend .heatmap-cell {
    width: 10px !important;
    height: 10px !important;
  }
}

@media (max-width: 480px) {
  .stats-controls {
    flex-direction: column;
    align-items: stretch;
  }
  
  .stats-select {
    width: 100%;
  }
}

/* 통계 페이지 헤더 정렬 */
#statistics-view .view-header,
.statistics-view-active .view-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  padding-bottom: 0;
  border-bottom: none;
}

#statistics-view .view-header h2,
.statistics-view-active .view-header h2 {
  margin: 0;
  line-height: 1;
}

#statistics-view .stats-controls,
.statistics-view-active .stats-controls {
  margin-bottom: 0;
  display: flex;
  align-items: center;
}
