/* ===== CSS Custom Properties ===== */
:root {
  --bg-page: #f5f4f0;
  --bg-surface: #ffffff;
  --bg-sidebar: #fafaf8;
  --bg-hover: #f0efeb;
  --bg-active: #e8f5f3;
  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --border: #e2e8f0;
  --accent: #0d9488;
  --accent-soft: #ccfbf1;
  --chart-weekday: #0d9488;
  --chart-saturday: #f59e0b;
  --chart-sunday: #ef4444;
  --sidebar-w: 380px;
  --header-h: 72px;
  --font-en: 'Urbanist', system-ui, sans-serif;
  --font-tc: 'Noto Sans TC', 'PingFang HK', 'Microsoft JhengHei', sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-page: #0b0f1a;
    --bg-surface: #131929;
    --bg-sidebar: #101525;
    --bg-hover: #1a2240;
    --bg-active: #0d2626;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --border: #1e2d4a;
    --accent: #2dd4bf;
    --accent-soft: #0d3331;
  }
}

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

[hidden] { display: none !important; }

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-en);
  background: var(--bg-page);
  color: var(--text-primary);
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
}

/* ===== Custom Scrollbar (desktop) ===== */
@media (min-width: 768px) {
  ::-webkit-scrollbar { width: 6px; }
  ::-webkit-scrollbar-track { background: transparent; }
  ::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
  }
  ::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }
}

/* ===== Accent Bar ===== */
.accent-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--accent) 0%, #f59e0b 50%, #ef4444 100%);
  z-index: 1000;
}

/* ===== Setup Message ===== */
.setup-message {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 2rem;
}
.setup-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 3rem;
  max-width: 480px;
  text-align: center;
}
.setup-card h1 {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 1rem;
}
.setup-card p { color: var(--text-secondary); margin-bottom: 0.75rem; }
.setup-hint {
  font-size: 0.82rem;
  color: var(--text-muted);
  font-style: italic;
}

/* ===== App Shell ===== */
.app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ===== Header ===== */
.header {
  position: sticky;
  top: 3px;
  z-index: 100;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  height: var(--header-h);
}
.header-inner {
  max-width: 1600px;
  margin: 0 auto;
  padding: 0 1.25rem;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.header-titles { display: flex; align-items: baseline; gap: 0.75rem; min-width: 0; }
.header-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.85rem;
  flex-shrink: 0;
}
.view-switch {
  display: inline-flex;
  gap: 4px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-hover);
}
.view-link {
  display: grid;
  gap: 0.04rem;
  padding: 0.38rem 0.8rem;
  border-radius: 999px;
  text-decoration: none;
  transition: background 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}
.view-link:hover {
  color: var(--text-primary);
}
.view-link:hover .view-link-en,
.view-link:hover .view-link-tc {
  color: var(--text-primary);
}
.view-link.is-current {
  background: var(--bg-surface);
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
  cursor: default;
}
.view-link-en {
  font-family: var(--font-en);
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.15;
}
.view-link-tc {
  font-family: var(--font-tc);
  font-size: 0.66rem;
  font-weight: 600;
  color: var(--text-secondary);
  line-height: 1.15;
}
.view-link.is-current .view-link-en,
.view-link.is-current .view-link-tc {
  color: var(--accent);
}
.header-sync {
  display: flex;
  align-items: center;
}
.header-title-tc {
  font-family: var(--font-tc);
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.15;
}
.header-title-en {
  font-family: var(--font-en);
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-secondary);
  line-height: 1.25;
}
.sync-label {
  display: inline-flex;
  align-items: center;
  padding: 0.28rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.04);
  font-size: 0.72rem;
  color: var(--text-muted);
  white-space: nowrap;
  letter-spacing: 0.02em;
}

/* ===== Layout ===== */
.layout {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: calc(100vh - var(--header-h) - 3px);
  position: sticky;
  top: calc(var(--header-h) + 3px);
}

.sidebar-filters {
  padding: 1rem 1rem 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  border-bottom: 1px solid var(--border);
}

/* Type Tabs */
.type-tabs {
  display: flex;
  gap: 4px;
  background: var(--bg-hover);
  border-radius: 8px;
  padding: 3px;
}
.type-tab {
  flex: 1;
  padding: 0.4rem 0.75rem;
  border: none;
  border-radius: 6px;
  background: transparent;
  font-family: var(--font-en);
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s ease;
}
.type-tab.active {
  background: var(--bg-surface);
  color: var(--accent);
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.type-tab:hover:not(.active) { color: var(--text-primary); }

/* District Select */
.district-select {
  width: 100%;
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-surface);
  font-family: var(--font-en);
  font-size: 0.78rem;
  color: var(--text-primary);
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%2364748b' viewBox='0 0 16 16'%3E%3Cpath d='M1.5 5.5l6.5 6 6.5-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.6rem center;
}
.district-select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

/* Search */
.search-input {
  width: 100%;
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-surface);
  font-family: var(--font-en);
  font-size: 0.78rem;
  color: var(--text-primary);
  transition: border-color 0.2s;
}
.search-input::placeholder { color: var(--text-muted); }
.search-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* Results count */
.results-count {
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding-bottom: 0.4rem;
}

/* Section List */
.section-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 0.5rem 0;
}

.section-item {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  padding: 0.65rem 1rem;
  border-left: 3px solid transparent;
  cursor: pointer;
  transition: all 0.15s ease;
  animation: itemFadeIn 0.25s ease both;
}
.section-item:hover { background: var(--bg-hover); }
.section-item.active {
  background: var(--bg-active);
  border-left-color: var(--accent);
}
.section-item.no-data {
  opacity: 0.38;
  filter: grayscale(100%);
}
.section-item.no-data .section-name-tc,
.section-item.no-data .section-name-en {
  text-decoration: line-through;
  color: var(--text-muted);
}

@keyframes itemFadeIn {
  from { opacity: 0; transform: translateX(-8px); }
  to { opacity: 1; transform: translateX(0); }
}

.section-item-body { flex: 1; min-width: 0; }
.section-live {
  margin-left: auto;
  min-width: 3.5rem;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  text-align: right;
}
.section-live-value {
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1;
  color: var(--accent);
}
.section-live-label {
  font-size: 0.58rem;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.section-live-secondary {
  margin-top: 0.28rem;
  display: flex;
  align-items: baseline;
  gap: 0.25rem;
}
.section-live-secondary-value {
  font-size: 1rem;
  font-weight: 800;
  line-height: 1;
  color: var(--text-primary);
}
.section-live-secondary-label {
  font-size: 0.74rem;
  font-weight: 800;
  color: var(--text-muted);
  letter-spacing: 0.04em;
}
.section-name-tc {
  font-family: var(--font-tc);
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text-primary);
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.section-name-en {
  font-size: 0.72rem;
  font-weight: 400;
  color: var(--text-secondary);
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.section-busiest-block {
  margin-top: 0.34rem;
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 0.55rem;
  min-width: 0;
}
.section-busiest-value {
  font-size: 1.02rem;
  font-weight: 800;
  line-height: 1.02;
  letter-spacing: -0.03em;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.section-busiest-value.dual {
  font-size: 0.92rem;
}
.section-busiest-copy {
  font-size: 0.58rem;
  font-weight: 700;
  line-height: 1.12;
  color: var(--text-secondary);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  text-align: left;
}
.section-badges {
  display: flex;
  gap: 0.35rem;
  margin-top: 0.25rem;
  flex-wrap: wrap;
}
.badge {
  font-size: 0.65rem;
  font-weight: 600;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  white-space: nowrap;
}
.badge-district {
  background: var(--bg-hover);
  color: var(--text-secondary);
}
.badge-spaces {
  background: var(--accent-soft);
  color: var(--accent);
}
.badge-nodata {
  font-style: italic;
  color: var(--text-muted);
  background: transparent;
  border: 1px dashed var(--border);
}

/* ===== Detail Panel ===== */
.detail-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  height: calc(100vh - var(--header-h) - 3px);
  background: var(--bg-page);
}

.detail-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 0.5rem;
}
.placeholder-icon { font-size: 3rem; opacity: 0.4; }
.placeholder-text-tc {
  font-family: var(--font-tc);
  font-size: 1rem;
  color: var(--text-muted);
}
.placeholder-text-en {
  font-size: 0.82rem;
  color: var(--text-muted);
}

.detail-content {
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  animation: contentFadeIn 0.3s ease;
}
@keyframes contentFadeIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

.back-btn {
  display: none;
  border: none;
  background: transparent;
  font-family: var(--font-en);
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--accent);
  cursor: pointer;
  padding: 0.3rem 0;
  align-self: flex-start;
}
.back-btn:hover { text-decoration: underline; }

.detail-header { display: flex; flex-direction: column; gap: 0.2rem; }
.detail-name-tc {
  font-family: var(--font-tc);
  font-size: 1.3rem;
  font-weight: 700;
}
.detail-name-en {
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-secondary);
}
.detail-meta {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.25rem;
}

/* Day toggles */
.day-toggles {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.day-toggle {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.35rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg-surface);
  font-family: var(--font-en);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
}
.day-toggle.active {
  border-color: transparent;
  color: var(--text-primary);
}
.day-toggle:not(.active) { opacity: 0.45; }

.toggle-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}
.weekday-dot { background: var(--chart-weekday); }
.saturday-dot { background: var(--chart-saturday); }
.sunday-dot { background: var(--chart-sunday); }

/* Chart */
.chart-wrap {
  position: relative;
  width: 100%;
  min-height: 340px;
  max-height: 520px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem;
  flex: 1;
}
.chart-wrap canvas { width: 100% !important; height: 100% !important; }

/* Loading */
.detail-loading {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  color: var(--text-muted);
}
.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* No data */
.detail-nodata {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 0.4rem;
}
.nodata-tc { font-family: var(--font-tc); color: var(--text-muted); font-size: 1rem; }
.nodata-en { color: var(--text-muted); font-size: 0.82rem; }

/* Detail Info (photo + basic info) */
.detail-info {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.detail-info-title {
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--text-secondary);
  letter-spacing: 0.02em;
  text-transform: uppercase;
}
.detail-note {
  margin: 0;
  font-size: 0.78rem;
  color: var(--text-secondary);
}
.detail-insights {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 0.95rem 1rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: linear-gradient(135deg, rgba(13, 110, 253, 0.08), rgba(32, 201, 151, 0.08));
}
.detail-insights-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 0.75rem;
}
.detail-insight-item {
  padding: 0.8rem 0.9rem;
  border-radius: 10px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  box-shadow: 0 8px 18px rgba(2, 6, 23, 0.08);
}
.detail-insight-label {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--text-secondary);
  line-height: 1.35;
}
.detail-insight-value {
  margin-top: 0.25rem;
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.25;
}
.detail-insight-meta {
  margin-top: 0.25rem;
  font-size: 0.74rem;
  color: var(--text-secondary);
  line-height: 1.35;
}
.detail-photo {
  width: min(100%, 500px);
  height: auto;
  max-height: none;
  object-fit: contain;
  align-self: flex-start;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.map-icon-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: 0.4rem;
  text-decoration: none;
  font-size: 0.95rem;
}
.map-icon-link:hover {
  text-decoration: none;
}
.detail-info-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.35rem 0.75rem;
  font-size: 0.78rem;
  line-height: 1.5;
}
.info-label {
  font-weight: 600;
  color: var(--text-secondary);
  white-space: nowrap;
}
.info-value {
  color: var(--text-primary);
  word-break: break-word;
}
.info-value a {
  color: var(--accent);
  text-decoration: none;
}
.info-value a:hover { text-decoration: underline; }

/* ===== Footer ===== */
.footer {
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
  text-align: center;
  padding: 1rem;
  font-size: 0.68rem;
  color: var(--text-muted);
  line-height: 1.6;
}
.footer-powered {
  margin-top: 0.25rem;
  font-weight: 600;
  color: var(--accent);
}

/* ===== Overlay backdrop (mobile) ===== */
.overlay-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 200;
  opacity: 0;
  transition: opacity 0.25s ease;
}
.overlay-backdrop.visible { opacity: 1; }

/* ===== Mobile styles ===== */
@media (max-width: 767px) {
  :root {
    --header-h: 116px;
  }
  .header-inner {
    height: 100%;
    min-height: var(--header-h);
    padding: 0.45rem 1rem 0.4rem;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    align-items: start;
    gap: 0.5rem;
  }
  .header-titles {
    display: grid;
    gap: 0.18rem;
    width: 100%;
  }
  .header-title-tc {
    font-size: 0.98rem;
  }
  .header-title-en {
    display: block;
    font-size: 0.68rem;
    max-width: none;
  }
  .header-actions {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 0.5rem;
  }
  .view-switch {
    width: 100%;
  }
  .view-link {
    justify-items: center;
    padding: 0.38rem 0.55rem;
  }
  .view-link-en {
    font-size: 0.68rem;
  }
  .view-link-tc {
    font-size: 0.62rem;
  }
  .header-sync {
    justify-self: end;
  }
  .sync-label {
    font-size: 0.64rem;
    padding: 0.18rem 0.5rem;
  }
  .sidebar {
    width: 100%;
    min-width: 100%;
    border-right: none;
    height: calc(100vh - var(--header-h) - 3px);
  }
  .detail-panel {
    position: fixed;
    inset: 0;
    top: 0;
    z-index: 250;
    background: var(--bg-page);
    transform: translateX(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    height: 100vh;
    padding-top: 3px;
  }
  .detail-panel.mobile-open {
    transform: translateX(0);
  }
  .back-btn { display: inline-flex; }
  .chart-wrap { min-height: 260px; }
}

/* ===== Large screens ===== */
@media (min-width: 1200px) {
  .chart-wrap { max-height: 600px; }
}
