/* ============================================================
   報名進度 Dashboard — Design System
   慈濟藍主色 · 長輩友善 · Mobile-first
   ============================================================ */

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

/* --- Design Tokens --- */
:root {
  /* Brand — 慈濟藍 */
  --brand: #1a4b8c;
  --brand-deep: #0e2f5a;
  --brand-light: #e8eef6;
  --brand-muted: #6889b3;

  /* Neutral — blue-tinted grays */
  --n-0: #ffffff;
  --n-50: #f5f7fa;
  --n-100: #ebeef3;
  --n-200: #d4dae3;
  --n-300: #b0bac8;
  --n-400: #8694a7;
  --n-500: #5f6f84;
  --n-600: #455568;
  --n-700: #2e3d4f;
  --n-800: #1c2a3a;
  --n-900: #111d2b;

  /* Semantic — 達成率 */
  --green: #1a8a42;
  --green-bg: #e6f5eb;
  --amber: #b3760a;
  --amber-bg: #fdf3e0;
  --red: #c4291c;
  --red-bg: #fce8e6;

  /* 5 群組 */
  --g-a-male: #2563a8;
  --g-a-female: #6ea8e0;
  --g-b-male: #c75d10;
  --g-b-female: #e8b44a;
  --g-c: #7c8998;

  /* Over (超標) */
  --over: #3478c6;
  --over-bg: #e5eef8;

  /* Spacing — 4pt base */
  --s-1: 4px;  --s-2: 8px;  --s-3: 12px;
  --s-4: 16px; --s-5: 20px; --s-6: 24px;
  --s-8: 32px; --s-10: 40px; --s-12: 48px;
  --s-16: 64px;

  /* Type */
  --font: 'Noto Sans TC', -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --text-xs: 0.8125rem;   /* 13px — only metadata */
  --text-sm: 0.875rem;    /* 14px — minimum */
  --text-base: 1rem;      /* 16px — body */
  --text-lg: 1.125rem;    /* 18px — emphasis */
  --text-xl: 1.375rem;    /* 22px — section titles */
  --text-2xl: 1.75rem;    /* 28px — KPI values */
  --text-3xl: 2.25rem;    /* 36px — hero numbers */
  --leading-tight: 1.25;
  --leading-normal: 1.5;
  --leading-relaxed: 1.65;

  /* Radius */
  --r-sm: 4px;
  --r-md: 6px;
  --r-lg: 10px;

  /* Shadow */
  --shadow-sm: 0 1px 2px rgba(14, 47, 90, 0.06);
  --shadow-md: 0 2px 8px rgba(14, 47, 90, 0.08);
}

/* --- Base --- */
body {
  font-family: var(--font);
  font-size: var(--text-base);
  line-height: var(--leading-relaxed);
  color: var(--n-800);
  background: var(--n-50);
  -webkit-font-smoothing: antialiased;
  font-kerning: normal;
}

/* --- Header --- */
.header {
  background: var(--brand-deep);
  position: sticky;
  top: 0;
  z-index: 100;
  padding: 0 var(--s-4);
  padding-top: env(safe-area-inset-top);
}

.header-inner {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.header-brand {
  color: var(--n-0);
  font-size: var(--text-lg);
  font-weight: 700;
  padding: var(--s-3) 0 var(--s-1);
  letter-spacing: 0.02em;
}

.tabs {
  display: flex;
  gap: var(--s-1);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding-bottom: 0;
}
.tabs::-webkit-scrollbar { display: none; }

.tab {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--brand-muted);
  font-family: var(--font);
  font-size: var(--text-sm);
  font-weight: 500;
  padding: var(--s-2) var(--s-3);
  cursor: pointer;
  white-space: nowrap;
  border-bottom: 3px solid transparent;
  transition: color 0.15s, border-color 0.15s;
  min-height: 44px;
  display: flex;
  align-items: center;
}

.tab:hover { color: rgba(255, 255, 255, 0.85); }

.tab.active {
  color: var(--n-0);
  border-bottom-color: var(--n-0);
  font-weight: 600;
}

/* --- Main --- */
.main {
  padding: var(--s-5) var(--s-4) var(--s-12);
  padding-left: max(var(--s-4), env(safe-area-inset-left));
  padding-right: max(var(--s-4), env(safe-area-inset-right));
}

.container {
  max-width: 1200px;
  margin: 0 auto;
}

/* --- Loading --- */
.loading {
  text-align: center;
  padding: var(--s-16) 0;
}
.loading-text {
  color: var(--n-400);
  font-size: var(--text-lg);
}

/* --- Pages --- */
.page { display: block; }
.page.hidden { display: none; }

.page-title {
  font-size: var(--text-xl);
  font-weight: 700;
  color: var(--brand-deep);
  margin-bottom: var(--s-5);
}

/* --- KPI Grid --- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--s-3);
  margin-bottom: var(--s-6);
}

.kpi {
  background: var(--n-0);
  border: 1px solid var(--n-100);
  border-radius: var(--r-md);
  padding: var(--s-4) var(--s-4) var(--s-3);
  box-shadow: var(--shadow-sm);
}

.kpi-label {
  font-size: var(--text-sm);
  color: var(--n-500);
  font-weight: 500;
  margin-bottom: var(--s-1);
}

.kpi-value {
  font-size: var(--text-2xl);
  font-weight: 700;
  line-height: var(--leading-tight);
  font-variant-numeric: tabular-nums;
}

.kpi--highlight {
  background: var(--brand-deep);
  border-color: var(--brand-deep);
}
.kpi--highlight .kpi-label { color: var(--brand-muted); }
.kpi--highlight .kpi-value { color: var(--n-0); }

/* --- Section --- */
.section {
  margin-bottom: var(--s-6);
}

.section-title {
  font-size: var(--text-base);
  font-weight: 600;
  color: var(--n-700);
  margin-bottom: var(--s-3);
  display: flex;
  align-items: center;
  gap: var(--s-2);
}

.section-title::before {
  content: '';
  width: 3px;
  height: 1em;
  background: var(--brand);
  border-radius: 2px;
  flex-shrink: 0;
}

/* --- Tables --- */
.table-wrap {
  background: var(--n-0);
  border: 1px solid var(--n-100);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-sm);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}

thead th {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--n-500);
  text-transform: none;
  letter-spacing: 0.02em;
  background: var(--n-50);
  padding: var(--s-2) var(--s-3);
  text-align: left;
  white-space: nowrap;
  border-bottom: 2px solid var(--n-200);
  position: sticky;
  top: 0;
  z-index: 1;
}

tbody td {
  padding: var(--s-2) var(--s-3);
  border-bottom: 1px solid var(--n-100);
  white-space: nowrap;
  color: var(--n-700);
}

tbody tr:last-child td { border-bottom: none; }

tbody tr:hover { background: var(--n-50); }

.num { text-align: right; }
th.num { text-align: right; }

.rank-cell {
  font-size: var(--text-xs);
  color: var(--n-400);
  font-weight: 600;
  width: 2rem;
}

/* --- Page 2: Harmony summary table --- */
.h-summary-row { cursor: pointer; }
.h-summary-row:hover { background: var(--n-50); }
.h-summary-row.is-open { background: var(--n-50); border-bottom: 2px solid var(--n-200); }
.h-summary-row .h-name { font-weight: 600; }

.toggle-cell {
  width: 24px;
  color: var(--n-400);
  font-size: var(--text-xs);
  text-align: center;
  user-select: none;
}
.h-summary-row.is-open .toggle-cell { color: var(--brand); }

.h-detail-row { background: var(--n-50); }
.h-detail-row td { padding-top: var(--s-1); padding-bottom: var(--s-1); font-size: var(--text-xs); color: var(--n-500); }
.h-session-name { padding-left: var(--s-4) !important; }

.district-header td {
  padding: var(--s-3) var(--s-3) var(--s-1);
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--n-400);
  letter-spacing: 0.04em;
  border-bottom: none;
}

/* Mini stacked bar */
.mini-bar {
  display: flex;
  height: 14px;
  border-radius: 2px;
  overflow: hidden;
  min-width: 100px;
  background: var(--n-100);
}
.mini-bar > div { min-width: 1px; }

.h-detail-groups { font-size: 10px; color: var(--n-500); white-space: nowrap; }

/* Desktop: show number cols, hide bar col */
.group-bar { display: none; }
.group-nums { display: table-cell; }

/* Mobile: show bar col, hide number cols */
@media (max-width: 639px) {
  .group-nums { display: none !important; }
  .group-bar { display: table-cell; }
}

/* --- Rate colors --- */
.rate-g { color: var(--green); font-weight: 600; }
.rate-y { color: var(--amber); font-weight: 600; }
.rate-r { color: var(--red); font-weight: 600; }

/* --- Rate badge --- */
.rate-badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: var(--r-sm);
  font-size: var(--text-xs);
  font-weight: 600;
}
.rate-badge--g { background: var(--green-bg); color: var(--green); }
.rate-badge--y { background: var(--amber-bg); color: var(--amber); }
.rate-badge--r { background: var(--red-bg); color: var(--red); }

/* --- Chart card --- */
.chart-card {
  background: var(--n-0);
  border: 1px solid var(--n-100);
  border-radius: var(--r-md);
  padding: var(--s-4);
  box-shadow: var(--shadow-sm);
  margin-bottom: var(--s-6);
}

.chart-card {
  overflow: hidden;
}

/* --- Filters --- */
.filters {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-3);
  margin-bottom: var(--s-5);
  padding: var(--s-4);
  background: var(--n-0);
  border: 1px solid var(--n-100);
  border-radius: var(--r-md);
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: var(--s-1);
  min-width: 140px;
  flex: 1;
}

.filter-label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--n-500);
  letter-spacing: 0.02em;
}

.filter-select {
  appearance: none;
  padding: var(--s-2) var(--s-8) var(--s-2) var(--s-3);
  border: 1px solid var(--n-200);
  border-radius: var(--r-sm);
  font-family: var(--font);
  font-size: var(--text-sm);
  color: var(--n-700);
  background: var(--n-0) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%235f6f84' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E") no-repeat right 10px center;
  min-height: 44px;
  cursor: pointer;
  transition: border-color 0.15s;
}

.filter-select:focus {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
  border-color: var(--brand);
}

/* --- Footer --- */
.footer {
  text-align: center;
  padding: var(--s-4) var(--s-4) var(--s-6);
  padding-bottom: max(var(--s-6), env(safe-area-inset-bottom));
  font-size: var(--text-xs);
  color: var(--n-400);
  display: flex;
  justify-content: center;
  align-items: center;
  gap: var(--s-3);
}

.btn-refresh {
  appearance: none;
  border: 1px solid var(--n-200);
  background: var(--n-0);
  color: var(--n-500);
  font-family: var(--font);
  font-size: var(--text-xs);
  padding: var(--s-1) var(--s-3);
  border-radius: var(--r-sm);
  cursor: pointer;
  min-height: 32px;
  transition: background 0.15s, color 0.15s;
}
.btn-refresh:hover { background: var(--n-50); color: var(--n-700); }
.btn-refresh:disabled { opacity: 0.5; cursor: not-allowed; }

/* --- Utility --- */
.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

.hidden { display: none !important; }

/* --- Two-col layout for wider screens --- */
.grid-2 {
  display: grid;
  gap: var(--s-5);
}

/* ============================================================
   Responsive — min-width (mobile-first)
   ============================================================ */

/* Tablet (640px+) */
@media (min-width: 640px) {
  .header-inner {
    flex-direction: row;
    align-items: stretch;
  }

  .header-brand {
    padding: var(--s-4) 0;
    margin-right: var(--s-6);
  }

  .tabs { align-items: stretch; }

  .tab {
    font-size: var(--text-base);
    padding: var(--s-3) var(--s-4);
  }

  .kpi-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: var(--s-4);
  }

  .kpi-value { font-size: var(--text-3xl); }

  .grid-2 { grid-template-columns: 1fr 1fr; }

  .filters { flex-wrap: nowrap; }
  .filter-group { min-width: 0; }
}

/* Desktop (1024px+) */
@media (min-width: 1024px) {
  .main { padding: var(--s-8) var(--s-6) var(--s-16); }

  .kpi-grid--6 { grid-template-columns: repeat(6, 1fr); }
  .kpi-grid--5 { grid-template-columns: repeat(5, 1fr); }
}

/* Touch-friendly targets */
@media (pointer: coarse) {
  .tab { min-height: 48px; }
  tbody td { padding: var(--s-3) var(--s-3); }
}
