* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg-0: #0f172a;
  --bg-1: #1a2332;
  --bg-2: #1e293b;
  --border: #334155;
  --text-1: #e2e8f0;
  --text-2: #94a3b8;
  --text-3: #64748b;
  --accent: #34d399;
  --accent-2: #10b981;
  --pikmin-red: #ef4444;
  --pikmin-blue: #3b82f6;
  --pikmin-yellow: #fbbf24;
  --warning: #f59e0b;
}

html, body {
  height: 100%;
  font-family: 'Microsoft JhengHei', 'PingFang TC', system-ui, sans-serif;
  background: var(--bg-0);
  color: var(--text-1);
  overflow: hidden;
}

/* Topbar */
.topbar {
  background: linear-gradient(135deg, #1a3a2f, #0f172a);
  padding: 12px 24px;
  border-bottom: 1px solid var(--bg-2);
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
}

.brand { display: flex; align-items: center; gap: 14px; }
.brand-icon { font-size: 28px; }
.brand h1 { font-size: 18px; font-weight: 800; }
.brand p { font-size: 12px; color: var(--text-2); margin-top: 2px; }

.icon-btn {
  background: var(--bg-1);
  color: var(--text-1);
  border: 1px solid var(--border);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
}
.icon-btn:hover { background: var(--bg-2); border-color: var(--accent); }
.topbar-actions { display: flex; gap: 8px; }

/* Ranking modal */
.modal-wide { width: 720px; max-width: 95vw; max-height: 85vh; display: flex; flex-direction: column; }
.ranking-body { display: grid; grid-template-columns: 280px 1fr; gap: 0; padding: 0; flex: 1; min-height: 0; }
.ranking-left, .ranking-right { padding: 16px; overflow-y: auto; }
.ranking-left { border-right: 1px solid var(--bg-2); }
.ranking-hint { font-size: 11px; color: var(--text-3); margin-bottom: 10px; }

.ranking-decor-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
}
.ranking-decor-row:hover { background: var(--bg-2); }
.ranking-decor-row.active { background: rgba(52, 211, 153, 0.15); border-left: 3px solid var(--accent); }
.ranking-decor-row .emoji { font-size: 18px; }
.ranking-decor-row .name { flex: 1; font-size: 13px; }
.ranking-decor-row .station-count {
  font-size: 11px;
  color: var(--text-2);
  background: var(--bg-2);
  padding: 2px 8px;
  border-radius: 10px;
}
.ranking-decor-row.rare .station-count { background: rgba(245, 158, 11, 0.2); color: var(--warning); }
.ranking-decor-row.exclusive .station-count { background: rgba(239, 68, 68, 0.2); color: var(--pikmin-red); font-weight: 700; }
.ranking-decor-row.no-data { opacity: 0.4; }
.ranking-decor-row.no-data .station-count { background: var(--bg-2); color: var(--text-3); }

.ranking-right h3 { font-size: 16px; font-weight: 700; margin-bottom: 4px; }
.ranking-right .summary-line { font-size: 12px; color: var(--text-2); margin-bottom: 12px; }

.station-rank-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s;
}
.station-rank-row:hover { background: var(--bg-2); }
.station-rank-row .rank { width: 30px; color: var(--text-3); font-variant-numeric: tabular-nums; }
.station-rank-row .rank.top1 { color: #fbbf24; font-weight: 700; }
.station-rank-row .rank.top3 { color: #d1d5db; font-weight: 700; }
.station-rank-row .station-name { flex: 1; }
.station-rank-row .lines { display: flex; gap: 3px; }
.station-rank-row .line-badge { font-size: 9px; padding: 1px 5px; }
.station-rank-row .count {
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  font-weight: 600;
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: 1fr 380px;
  height: calc(100vh - 64px);
}

.map-pane { position: relative; }
#map { width: 100%; height: 100%; background: var(--bg-1); }

.info-pane {
  background: var(--bg-1);
  border-left: 1px solid var(--bg-2);
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Mode tabs */
.mode-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--bg-2);
  border-radius: 8px;
  padding: 4px;
  gap: 4px;
}
.mode-tab {
  background: transparent;
  color: var(--text-2);
  border: none;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.mode-tab:hover { color: var(--text-1); }
.mode-tab.active {
  background: var(--bg-0);
  color: var(--accent);
}
.mode-panel { display: flex; flex-direction: column; gap: 16px; }

/* Route picker */
.route-picker { display: flex; flex-direction: column; gap: 8px; }
.route-picker label { font-size: 12px; color: var(--text-2); margin-top: 4px; }
.route-picker input {
  width: 100%;
  background: var(--bg-2);
  color: var(--text-1);
  border: 1px solid var(--border);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
}
.route-picker input:focus { outline: none; border-color: var(--accent); }
.full-btn { width: 100%; padding: 10px; font-size: 13px; margin-top: 4px; }

.route-type-selector {
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 6px;
}
.route-type-selector label {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; color: var(--text-1); cursor: pointer; margin: 0;
}
.route-type-selector input[type="radio"] { accent-color: var(--accent); }

.route-stats {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 6px;
}
.stat-tile {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 6px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.stat-num { font-size: 18px; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; }
.stat-label { font-size: 10px; color: var(--text-3); }

/* Route line on map */
.route-line { stroke: #10b981; stroke-width: 5; stroke-opacity: 0.85; }

/* Station picker */
.station-picker label {
  display: block;
  font-size: 12px;
  color: var(--text-2);
  margin-bottom: 6px;
}
.station-picker input {
  width: 100%;
  background: var(--bg-2);
  color: var(--text-1);
  border: 1px solid var(--border);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
}
.station-picker input:focus {
  outline: none;
  border-color: var(--accent);
}

/* Station info */
.station-info {
  background: var(--bg-2);
  border-radius: 10px;
  padding: 14px;
  border: 1px solid var(--border);
}
.station-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 10px;
}
.station-header h2 { font-size: 22px; font-weight: 800; }

.lines { display: flex; gap: 4px; }
.line-badge {
  background: var(--line-color, #888);
  color: white;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 6px;
  letter-spacing: 0.5px;
}

.station-stats {
  display: flex;
  gap: 16px;
  font-size: 12px;
  color: var(--text-2);
}
.station-stats span { color: var(--accent); font-weight: 700; font-size: 14px; }

/* Ridership block */
.ridership-block {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
}
.ridership-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.ridership-title { font-size: 12px; color: var(--text-2); }
.crowd-badge {
  font-size: 10px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 10px;
  border: 1px solid currentColor;
}
.ridership-stats {
  display: flex;
  gap: 14px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.ridership-stats > div { display: flex; flex-direction: column; gap: 2px; }
.r-num { font-size: 16px; font-weight: 700; color: var(--text-1); font-variant-numeric: tabular-nums; }
.r-unit { font-size: 10px; color: var(--text-3); }
.ridership-spark { margin-top: 4px; }

.route-station-rows { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.route-station-row {
  display: grid;
  grid-template-columns: 70px 32px 1fr 50px;
  gap: 8px;
  align-items: center;
  font-size: 12px;
}
.rs-name { color: var(--text-1); }
.rs-rank { color: var(--text-3); font-size: 10px; font-variant-numeric: tabular-nums; }
.rs-bar {
  height: 6px;
  background: var(--bg-0);
  border-radius: 3px;
  overflow: hidden;
}
.rs-bar-fill { display: block; height: 100%; border-radius: 3px; }
.rs-num { text-align: right; color: var(--text-2); font-variant-numeric: tabular-nums; }

/* Decor list */
.decor-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.decor-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: all 0.2s;
  cursor: default;
}
.decor-card.confidence-medium { opacity: 0.85; }
.decor-card.confidence-low { opacity: 0.65; border-style: dashed; }
.decor-card { cursor: pointer; }
.decor-card:hover {
  background: var(--bg-0);
  border-color: var(--accent);
}
.decor-card.active {
  background: rgba(52, 211, 153, 0.15);
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(52, 211, 153, 0.25);
}

/* POI map pins */
.poi-pin-wrap { background: none; border: none; }
.poi-pin {
  width: 28px; height: 28px;
  background: white;
  border: 2px solid var(--accent);
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
  cursor: pointer;
}
.poi-pin > span { transform: rotate(45deg); display: block; }

/* ════════════════════════════════════════
   手機 RWD（≤ 768px）
   ════════════════════════════════════════ */
@media (max-width: 768px) {
  /* Topbar 縮小 */
  .topbar {
    padding: 8px 14px;
    height: auto;
    min-height: 52px;
  }
  .brand-icon { font-size: 22px; }
  .brand h1 { font-size: 15px; }
  .brand p { display: none; }
  .icon-btn { padding: 6px 10px; font-size: 11px; }

  /* 主版面：垂直堆疊 */
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: 45vh 1fr;
    height: calc(100vh - 52px);
  }
  .info-pane {
    border-left: none;
    border-top: 1px solid var(--bg-2);
    padding: 12px;
    gap: 12px;
  }

  /* 文字、卡片縮一點 */
  .station-info { padding: 12px; }
  .station-header h2 { font-size: 19px; }

  /* 路線統計 4 格 → 2x2 */
  .route-stats { grid-template-columns: 1fr 1fr; gap: 6px; }
  .stat-num { font-size: 16px; }

  /* Modal 全螢幕 */
  .modal-content {
    width: 100vw !important;
    max-width: 100vw !important;
    max-height: 100vh;
    height: 100vh;
    border-radius: 0;
    display: flex;
    flex-direction: column;
  }
  .modal-body { overflow-y: auto; flex: 1; }

  /* 排行 modal：左右改上下 */
  .ranking-body {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }
  .ranking-left {
    border-right: none;
    border-bottom: 1px solid var(--bg-2);
    max-height: 35vh;
  }

  /* 沿路車站 row：縮欄寬 */
  .route-station-row { grid-template-columns: 1fr 30px 1fr 50px; gap: 6px; font-size: 11px; }

  /* Decor list 維持 2 欄但縮 padding */
  .decor-card { padding: 8px 10px; }
  .decor-emoji { font-size: 18px; }
  .decor-name { font-size: 12px; }
  .decor-count { font-size: 10px; }
}

/* 浮動 footer */
.site-footer {
  position: fixed;
  bottom: 6px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.78);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  color: var(--text-3);
  font-size: 11px;
  padding: 5px 12px;
  border-radius: 999px;
  border: 1px solid rgba(148, 163, 184, 0.15);
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 500;
  white-space: nowrap;
  pointer-events: auto;
}
.site-footer strong { color: var(--text-2); font-weight: 600; }
.site-footer a { color: var(--text-3); text-decoration: none; }
.site-footer a:hover { color: var(--accent); }
.site-footer .sep { color: var(--bg-2); }

@media (max-width: 480px) {
  .site-footer {
    bottom: 4px;
    font-size: 10px;
    padding: 4px 10px;
    gap: 6px;
  }
}

/* 手機橫向 / 平板：不堆疊但縮側邊欄 */
@media (min-width: 769px) and (max-width: 1024px) {
  .layout { grid-template-columns: 1fr 320px; }
}
.decor-emoji { font-size: 20px; }
.decor-info { flex: 1; }
.decor-name { font-size: 13px; font-weight: 600; }
.decor-count { font-size: 11px; color: var(--text-2); }
.decor-card[data-conf="low"] .decor-count::after { content: ' (推測)'; color: var(--warning); }

/* AI panel */
.ai-panel {
  background: linear-gradient(135deg, rgba(52, 211, 153, 0.06), rgba(16, 185, 129, 0.02));
  border: 1px solid rgba(52, 211, 153, 0.2);
  border-radius: 10px;
  padding: 14px;
}
.ai-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 12px;
}
.ai-header h3 { font-size: 14px; color: var(--accent); }
.primary-btn {
  background: var(--accent);
  color: var(--bg-0);
  border: none;
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}
.primary-btn:hover { background: var(--accent-2); }
.primary-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.ai-output {
  font-size: 13px;
  line-height: 1.7;
  color: #cbd5e1;
}
.ai-output p { margin: 0 0 10px; }
.ai-output p:last-child { margin-bottom: 0; }
.ai-output h2, .ai-output h3, .ai-output h4, .ai-output h5 {
  color: var(--accent);
  font-weight: 700;
  margin: 14px 0 6px;
}
.ai-output h2 { font-size: 16px; }
.ai-output h3 { font-size: 14px; }
.ai-output h4, .ai-output h5 { font-size: 13px; }
.ai-output h2:first-child, .ai-output h3:first-child { margin-top: 0; }
.ai-output strong { color: var(--text-1); font-weight: 700; }
.ai-output em { color: var(--text-1); font-style: italic; }
.ai-output ul { margin: 6px 0 10px 18px; padding: 0; }
.ai-output li { margin: 3px 0; }
.ai-output .hint { color: var(--text-3); font-size: 12px; }

.spinner {
  display: inline-block;
  width: 14px; height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Data status */
.data-status {
  font-size: 11px;
  color: var(--text-3);
  text-align: center;
  padding: 8px;
}

/* Modal */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
}
.modal.hidden, .hidden { display: none !important; }

.modal-content {
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 460px;
  max-width: 90vw;
}
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--bg-2);
}
.modal-header h2 { font-size: 16px; font-weight: 700; }
.close-btn {
  background: none; border: none; color: var(--text-2);
  font-size: 18px; cursor: pointer;
}

.modal-body { padding: 20px; }
.form-row { margin-bottom: 16px; }
.form-row label {
  display: block;
  font-size: 12px;
  color: var(--text-2);
  margin-bottom: 6px;
}
.form-row input, .form-row select {
  width: 100%;
  background: var(--bg-2);
  color: var(--text-1);
  border: 1px solid var(--border);
  padding: 9px 12px;
  border-radius: 8px;
  font-size: 13px;
}
.checkbox-row label { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.checkbox-row input { width: auto; }

.hint-link {
  display: inline-block;
  font-size: 11px;
  color: var(--accent);
  margin-top: 6px;
  text-decoration: none;
}
.hint-link:hover { text-decoration: underline; }

.modal-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  gap: 8px;
}
.ghost-btn {
  background: transparent;
  color: var(--text-2);
  border: 1px solid var(--border);
  padding: 8px 14px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
}
.ghost-btn:hover { color: var(--pikmin-red); border-color: var(--pikmin-red); }

.warning-text {
  font-size: 11px;
  color: var(--warning);
  margin-top: 14px;
  line-height: 1.5;
}

/* Leaflet 自訂 marker */
.station-marker {
  background: white;
  border: 3px solid var(--accent);
  border-radius: 50%;
  width: 14px; height: 14px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
  cursor: pointer;
}
.station-marker.active {
  background: var(--accent);
  width: 20px; height: 20px;
  box-shadow: 0 0 0 6px rgba(52, 211, 153, 0.3);
}
.leaflet-popup-content-wrapper {
  background: var(--bg-1);
  color: var(--text-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0;
}
.leaflet-popup-tip { background: var(--bg-1); border: 1px solid var(--border); }
.leaflet-popup-content { margin: 12px 14px; }
.leaflet-popup-close-button { color: var(--text-2) !important; }

.poi-popup-header {
  display: flex; align-items: flex-start; gap: 10px;
  margin-bottom: 8px;
}
.poi-popup-emoji {
  font-size: 24px;
  line-height: 1;
}
.poi-popup-name {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-1);
  line-height: 1.3;
}
.poi-popup-decor {
  font-size: 11px;
  color: var(--accent);
  margin-top: 2px;
}
.poi-popup-details {
  margin-bottom: 8px;
  padding: 8px 0;
  border-top: 1px solid var(--bg-2);
  border-bottom: 1px solid var(--bg-2);
}
.poi-popup-row {
  font-size: 12px;
  color: var(--text-1);
  padding: 2px 0;
  line-height: 1.5;
}
.poi-popup-row a { color: var(--accent); text-decoration: none; }
.poi-popup-row a:hover { text-decoration: underline; }

.poi-popup-coord {
  font-size: 11px;
  color: var(--text-3);
  font-family: monospace;
  margin-bottom: 10px;
  padding: 6px 8px;
  background: var(--bg-2);
  border-radius: 5px;
}
.poi-popup-actions {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.poi-popup-btn {
  display: block;
  text-align: center;
  background: var(--bg-2);
  color: var(--text-1) !important;
  border: 1px solid var(--border);
  padding: 7px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  text-decoration: none;
  transition: all 0.15s;
}
.poi-popup-btn:hover { border-color: var(--accent); background: var(--bg-0); }
.poi-popup-btn.primary {
  background: var(--accent);
  color: var(--bg-0) !important;
  border-color: var(--accent);
}
.poi-popup-btn.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
