/* ==================== 統計分析模組 (analytics.css) ==================== */
/* 從 AnalyticsTab.html 遷移 */

/* ==================== 分析頁籤整體樣式 ==================== */
#analyticsTab {
  padding: 0;
}

/* 篩選器區塊 */
.analytics-filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  padding: 16px 20px;
  background: white;
  border-radius: 16px;
  margin-bottom: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
}

.analytics-filter-bar label {
  font-size: 13px;
  font-weight: 500;
  color: #86868b;
  margin: 0;
  white-space: nowrap;
}

.analytics-filter-bar select,
.analytics-filter-bar input[type="date"] {
  padding: 8px 12px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  font-size: 14px;
  min-width: 140px;
  background: white;
}

.analytics-filter-bar select:focus,
.analytics-filter-bar input[type="date"]:focus {
  border-color: #007AFF;
  outline: none;
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.1);
}

.filter-btn {
  padding: 8px 20px;
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  color: white;
  background: linear-gradient(180deg, #007AFF 0%, #0051D5 100%);
  cursor: pointer;
  transition: all 0.2s ease;
}

.filter-btn:hover {
  background: linear-gradient(180deg, #0051D5 0%, #003DB5 100%);
  transform: translateY(-1px);
}

.filter-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* ==================== 儀表板卡片區 ==================== */
.dashboard-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.dashboard-card {
  background: white;
  border-radius: 16px;
  padding: 20px;
  text-align: center;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  transition: all 0.2s ease;
}

.dashboard-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.dashboard-card.alert-card {
  border-left: 4px solid #FF3B30;
  background: linear-gradient(135deg, #fff5f5 0%, #ffe5e5 100%);
}

.dashboard-card .card-value {
  font-size: 32px;
  font-weight: 700;
  color: #1d1d1f;
  line-height: 1.2;
}

.dashboard-card .card-value .unit {
  font-size: 14px;
  font-weight: 500;
  color: #86868b;
  margin-left: 4px;
}

.dashboard-card .card-label {
  font-size: 13px;
  color: #86868b;
  margin-top: 8px;
  font-weight: 500;
}

.dashboard-card .card-sub {
  font-size: 12px;
  color: #34C759;
  margin-top: 4px;
}

.dashboard-card .card-sub.warning {
  color: #FF9500;
}

.dashboard-card .card-sub.danger {
  color: #FF3B30;
}

/* v49.3.26: MoM 增長率標籤 */
.mom-info {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 11px;
  margin-top: 6px;
  font-weight: 600;
}

.mom-up {
  color: #34C759;
}

.mom-down {
  color: #FF3B30;
}

.mom-label {
  color: #86868b;
  font-weight: 400;
}

/* 管理按鈕與面版 */
.admin-actions {
  margin-left: auto;
  display: flex;
  gap: 8px;
}

.admin-btn {
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid rgba(0, 0, 0, 0.1);
  background: #f5f5f7;
  color: #1d1d1f;
  transition: all 0.2s;
}

.admin-btn:hover {
  background: #e8e8ed;
}

/* 彈出面版樣式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.modal-content {
  background: white;
  width: 90%;
  max-width: 400px;
  border-radius: 20px;
  padding: 24px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
}

.close-modal {
  cursor: pointer;
  font-size: 24px;
  color: #86868b;
}

/* ==================== 圖表區域 ==================== */
.charts-section {
  margin-bottom: 24px;
}

.chart-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}

.chart-container {
  background: white;
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
}

.chart-container.full-width {
  grid-column: 1 / -1;
}

.chart-title {
  font-size: 16px;
  font-weight: 600;
  color: #1d1d1f;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.chart-title .chart-icon {
  width: 24px;
  height: 24px;
  margin-right: 8px;
}

.chart-canvas-wrapper {
  position: relative;
  height: 280px;
}

.chart-canvas-wrapper.tall {
  height: 400px;
}

/* 圖表載入中 */
.chart-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
  color: #86868b;
}

.chart-loading .spinner {
  width: 32px;
  height: 32px;
  border: 3px solid rgba(0, 122, 255, 0.2);
  border-top-color: #007AFF;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-right: 12px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ==================== Top 20 展開樣式 v49.2.0 ==================== */
.top-items-list {
  max-height: 400px;
  overflow-y: auto;
}

.top-item-row {
  display: flex;
  align-items: center;
  padding: 8px 10px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  transition: background 0.2s;
}

.top-item-row:hover {
  background: #f8f9fa;
}

.top-item-row:last-child {
  border-bottom: none;
}

.top-item-rank {
  width: 26px;
  height: 26px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  margin-right: 10px;
  background: #f0f0f0;
  color: #666;
  flex-shrink: 0;
}

.top-item-rank.rank-1 {
  background: linear-gradient(135deg, #FFD700, #FFA500);
  color: #333;
}

.top-item-rank.rank-2 {
  background: linear-gradient(135deg, #C0C0C0, #A0A0A0);
  color: #fff;
}

.top-item-rank.rank-3 {
  background: linear-gradient(135deg, #CD7F32, #8B4513);
  color: #fff;
}

/* v49.2.0: 熱門品項資訊區塊 - 允許換行 */
.top-item-info {
  flex: 1;
  min-width: 0;
  padding-right: 8px;
}

/* v49.2.0: 熱門品項名稱 - 允許換行，限制寬度 */
.top-item-name {
  font-weight: 600;
  font-size: 13px;
  color: #1d1d1f;
  line-height: 1.3;
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.top-item-code {
  font-size: 11px;
  color: #86868b;
  margin-top: 2px;
}

/* v49.2.0: 熱門品項數值 - 固定寬度 */
.top-item-value {
  font-size: 14px;
  font-weight: 700;
  color: #ff6d01;
  min-width: 40px;
  text-align: right;
  flex-shrink: 0;
}

.top-item-value .unit {
  font-size: 11px;
  font-weight: 500;
  color: #86868b;
}

/* v49.2.0: 熱門品項進度條 - 縮小寬度 */
.top-item-bar {
  width: 50px;
  height: 6px;
  background: #e9ecef;
  border-radius: 3px;
  margin-left: 8px;
  overflow: hidden;
  flex-shrink: 0;
}

.top-item-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #ff6d01, #ff9500);
  border-radius: 3px;
}

/* 展開更多按鈕 */
.expand-more-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 12px;
  margin-top: 8px;
  border: 1px dashed #007AFF;
  border-radius: 10px;
  background: transparent;
  color: #007AFF;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.expand-more-btn:hover {
  background: rgba(0, 122, 255, 0.05);
}

.expand-more-btn .arrow {
  margin-left: 6px;
  transition: transform 0.3s;
}

.expand-more-btn.expanded .arrow {
  transform: rotate(180deg);
}

.hidden-items {
  display: none;
}

.hidden-items.show {
  display: block;
}

/* ==================== 警示清單區 ==================== */
.alert-section {
  margin-bottom: 24px;
}

.alert-list-container {
  background: white;
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  margin-bottom: 16px;
}

.alert-list-title {
  font-size: 16px;
  font-weight: 600;
  color: #1d1d1f;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
}

.alert-list-title .badge {
  margin-left: 8px;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}

.alert-list-title .badge-danger {
  background: #FF3B30;
  color: white;
}

.alert-list-title .badge-warning {
  background: #FF9500;
  color: white;
}

.alert-list-title .badge-info {
  background: #007AFF;
  color: white;
}

.alert-list {
  max-height: 300px;
  overflow-y: auto;
}

.alert-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-radius: 10px;
  margin-bottom: 8px;
  background: #f8f9fa;
  transition: all 0.2s ease;
}

.alert-item:hover {
  background: #e9ecef;
}

.alert-item.overdue {
  background: linear-gradient(135deg, #fff5f5 0%, #ffe5e5 100%);
  border-left: 3px solid #FF3B30;
}

.alert-item.today {
  background: linear-gradient(135deg, #fff8e1 0%, #ffecb3 100%);
  border-left: 3px solid #FF9500;
}

.alert-item.week {
  background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
  border-left: 3px solid #007AFF;
}

.alert-item .customer-info {
  flex: 1;
}

.alert-item .customer-name {
  font-weight: 600;
  color: #1d1d1f;
  font-size: 14px;
}

.alert-item .customer-meta {
  font-size: 12px;
  color: #86868b;
  margin-top: 2px;
}

.alert-item .follow-info {
  text-align: right;
}

.alert-item .follow-date {
  font-size: 13px;
  font-weight: 600;
}

.alert-item .follow-action {
  font-size: 12px;
  color: #86868b;
  margin-top: 2px;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 高頻客戶排名 */
.rank-list .rank-item {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.rank-list .rank-item:last-child {
  border-bottom: none;
}

.rank-item .rank-number {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  margin-right: 12px;
}

.rank-item .rank-number.gold {
  background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
  color: #333;
}

.rank-item .rank-number.silver {
  background: linear-gradient(135deg, #C0C0C0 0%, #A0A0A0 100%);
  color: white;
}

.rank-item .rank-number.bronze {
  background: linear-gradient(135deg, #CD7F32 0%, #8B4513 100%);
  color: white;
}

.rank-item .rank-number.normal {
  background: #f0f0f0;
  color: #666;
}

.rank-item .rank-info {
  flex: 1;
}

.rank-item .rank-name {
  font-weight: 600;
  font-size: 14px;
  color: #1d1d1f;
}

.rank-item .rank-meta {
  font-size: 12px;
  color: #86868b;
}

.rank-item .rank-value {
  font-size: 18px;
  font-weight: 700;
  color: #007AFF;
}

.rank-item .rank-value .unit {
  font-size: 12px;
  font-weight: 500;
  color: #86868b;
}

/* 低效業務員警示 */
.efficiency-alert {
  background: linear-gradient(135deg, #fff5f5 0%, #ffe5e5 100%);
  border: 1px solid #ffcdd2;
}

.efficiency-alert .alert-item {
  background: rgba(255, 255, 255, 0.8);
}

/* ==================== 無權限提示 ==================== */
.no-permission {
  text-align: center;
  padding: 60px 20px;
  color: #86868b;
}

.no-permission .icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.no-permission h3 {
  color: #1d1d1f;
  margin-bottom: 8px;
}

.no-permission p {
  font-size: 14px;
}

/* ==================== 響應式設計 ==================== */
@media (max-width: 768px) {
  .analytics-filter-bar {
    flex-direction: column;
    align-items: stretch;
  }

  .analytics-filter-bar>* {
    width: 100%;
  }

  .dashboard-cards {
    grid-template-columns: repeat(2, 1fr);
  }

  .chart-row {
    grid-template-columns: 1fr;
  }

  .dashboard-card .card-value {
    font-size: 24px;
  }

  .chart-canvas-wrapper {
    height: 250px;
  }

  /* v49.2.0: 手機版熱門品項調整 */
  .top-item-row {
    padding: 6px 8px;
  }

  .top-item-name {
    font-size: 12px;
  }

  .top-item-bar {
    width: 40px;
  }
}

/* ==================== PDF 匯出排版優化 ==================== */
/* 使用 class 控制，確保 html2pdf 能抓到樣式 */
.printing-mode {
  background-color: white !important;
  padding: 20px !important;
  /* 強制固定寬度，避免寬螢幕下內容被縮小得太嚴重 */
  width: 1050px !important;
  margin: 0 auto !important;
}

/* 增加文字大小以提升閱讀體驗 */
.printing-mode .card-label {
  font-size: 16px !important;
}
.printing-mode .card-value {
  font-size: 28px !important;
}
.printing-mode .chart-title {
  font-size: 18px !important;
  margin-bottom: 12px !important;
}
.printing-mode .alert-list-title {
  font-size: 18px !important;
}
.printing-mode .alert-item {
  padding: 8px 12px !important;
}
.printing-mode .alert-item .customer-name {
  font-size: 15px !important;
}
.printing-mode .rank-item .rank-name {
  font-size: 15px !important;
}

/* 儀表板卡片改用 Flex */
.printing-mode .dashboard-cards {
  display: flex !important;
  flex-wrap: wrap !important;
  gap: 12px !important;
  page-break-inside: avoid !important;
}

.printing-mode .dashboard-card {
  flex: 1 1 140px !important;
  margin: 0 !important;
  page-break-inside: avoid !important;
  break-inside: avoid !important;
  box-shadow: none !important;
  border: 1px solid #eee !important;
}

/* 圖表列改用 Flex */
.printing-mode .chart-row {
  display: flex !important;
  flex-wrap: wrap !important;
  justify-content: space-between !important;
  gap: 0 !important;
  margin-bottom: 20px !important;
  page-break-inside: auto !important;
}

/* 圖表容器強制不分頁 */
.printing-mode .chart-container {
  width: 48% !important;
  flex: 0 0 48% !important;
  margin-bottom: 15px !important;
  box-shadow: none !important;
  border: 1px solid #eee !important;
  page-break-inside: avoid !important;
  break-inside: avoid !important;
  display: block !important;
}

/* 全寬圖表 */
.printing-mode .chart-container.full-width {
  width: 100% !important;
  flex: 0 0 100% !important;
  margin-bottom: 15px !important;
}

/* 警示清單區塊 */
.printing-mode .alert-list-container {
  page-break-inside: avoid !important;
  break-inside: avoid !important;
  box-shadow: none !important;
  border: 1px solid #eee !important;
}

/* 確保標題跟內容不分離 */
.printing-mode .chart-title,
.printing-mode .alert-list-title {
  page-break-after: avoid !important;
  break-after: avoid !important;
}

/* 隱藏 PDF 不需要看到的元素 */
.printing-mode .analytics-filter-bar,
.printing-mode .expand-more-btn {
  display: none !important;
}

/* 展開所有隱藏項目以便列印 */
.printing-mode .hidden-items {
  display: block !important;
}

/* 圖表高度調整，避免過高佔版面 */
.printing-mode .chart-canvas-wrapper {
  height: 250px !important;
}
.printing-mode .chart-canvas-wrapper.tall {
  height: 350px !important;
}

@media print {
  .chart-row,
  .alert-list-container,
  .dashboard-card,
  .top-items-list {
    page-break-inside: avoid !important;
    break-inside: avoid !important;
  }

  .chart-title,
  .alert-list-title {
    page-break-after: avoid !important;
    break-after: avoid !important;
  }

  /* 避免圖表被切斷 */
  .chart-container {
    page-break-inside: avoid !important;
  }
}
