:root {
  --panel-bg: rgba(20, 24, 28, 0.75);
  --panel-border: rgba(255, 255, 255, 0.12);
  --text: #f4f6fb;
  --muted: #c2c8d6;
  --accent: #4ade80;
  --shadow: 0 12px 30px rgba(0, 0, 0, 0.35);
  --radius: 14px;
  --blur: blur(12px);
}

* {
  box-sizing: border-box;
}

body, html {
  margin: 0;
  height: 100%;
  width: 100%;
  font-family: "Noto Sans TC", "Segoe UI", sans-serif;
  background: #0b1118;
  color: var(--text);
}

#map {
  position: absolute;
  inset: 0;
}

.controls {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  width: min(1000px, 92vw);
  background: var(--panel-bg);
  backdrop-filter: var(--blur);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 18px;
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  gap: 14px;
  align-items: center;
}

.controls-left,
.controls-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.controls-center {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#play-toggle {
  background: linear-gradient(135deg, #22c55e, #16a34a);
  border: none;
  color: #0b1118;
  padding: 10px 14px;
  border-radius: 10px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease;
  box-shadow: 0 6px 16px rgba(34, 197, 94, 0.35);
}

#play-toggle:hover {
  transform: translateY(-1px);
}

.date-display {
  font-variant-numeric: tabular-nums;
  font-size: 15px;
  letter-spacing: 0.5px;
}

#date-slider {
  width: 100%;
  accent-color: #22c55e;
}

.range-labels {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--muted);
}

#speed-select {
  background: #121922;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 8px 10px;
}

.controls label {
  font-size: 13px;
  color: var(--muted);
}

.legend {
  position: fixed;
  top: 14px;
  right: 14px;
  width: 260px;
  background: var(--panel-bg);
  backdrop-filter: var(--blur);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
}

.legend-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  font-weight: 700;
}

#legend-items {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 10px;
  font-size: 13px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  border: 1px solid rgba(0, 0, 0, 0.2);
}

.legend-name {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.legend-name small {
  color: var(--muted);
  font-size: 11px;
}


/* ── 系統選擇面板 ─────────────────────────────── */
#sys-toggle {
  background: #1e2a44;
  color: #cdd8f0;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 6px 10px;
  font: inherit;
  cursor: pointer;
}
#sys-toggle:hover { background: #294067; }

.sys-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  background: rgba(4, 8, 16, 0.55);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
}
.sys-overlay[hidden] { display: none; }

.sys-card {
  width: min(680px, 92vw);
  max-height: 82vh;
  display: flex;
  flex-direction: column;
  background: rgba(16, 20, 32, 0.97);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 16px 18px;
  color: var(--text);
}
.sys-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 10px;
}
.sys-head button {
  background: none; border: none; color: var(--muted);
  font-size: 18px; cursor: pointer;
}
.sys-all {
  display: block;
  padding: 8px 10px;
  background: #1a2740;
  border-radius: 8px;
  margin-bottom: 10px;
  cursor: pointer;
}
.sys-list {
  overflow-y: auto;
  flex: 1;
}
.sys-grp {
  font-size: 12px;
  color: var(--muted);
  margin: 10px 2px 4px;
  letter-spacing: 0.05em;
}
.sys-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  width: 32%;
  padding: 4px 6px;
  font-size: 13px;
  cursor: pointer;
  vertical-align: top;
}
.sys-item i { color: var(--muted); font-style: normal; font-size: 11px; }
.sys-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
  gap: 10px;
}
#sys-hint { font-size: 12px; color: var(--muted); }
#sys-apply {
  background: linear-gradient(135deg, #5b8cff, #3a6de0);
  color: #06122e;
  font-weight: 700;
  border: none;
  border-radius: 8px;
  padding: 8px 20px;
  cursor: pointer;
}
/* ≤640 的系統面板單欄佈局已由上方 ≤768 手機 sheet 統一處理 */

/* ── 手機 / 平板直向斷點(≤768px):純附加,桌面版完全不動 ─────────── */
@media (max-width: 768px) {
  html, body { overflow-x: hidden; }

  /* 頂部國家 tab:整條全寬、可橫向捲動;文字不換行、不再上下疊字 */
  #region-switch {
    left: 8px !important;
    right: 8px !important;
    top: 8px !important;
    transform: none !important;
    max-width: none !important;
    justify-content: flex-start;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  #region-switch::-webkit-scrollbar { height: 0; display: none; }
  #region-switch a { white-space: nowrap; flex: 0 0 auto; }

  /* 地圖縮放鈕下移,讓開國家列 */
  .maplibregl-ctrl-top-left { top: 46px !important; }

  /* 圖例:收合成右上可點小徽章,展開才顯示線色 */
  .legend {
    top: 46px;
    right: 8px;
    left: auto;
    width: auto;
    max-width: 78vw;
    padding: 9px 12px;
  }
  .legend-header {
    margin-bottom: 8px;
    cursor: pointer;
    gap: 8px;
    justify-content: flex-start;
  }
  .legend-header::after {
    content: "▾";
    margin-left: auto;
    color: var(--muted);
    font-size: 12px;
  }
  .legend.legend-collapsed .legend-header { margin-bottom: 0; }
  .legend.legend-collapsed .legend-header::after { content: "▸"; }
  .legend.legend-collapsed #legend-items { display: none; }
  #legend-items {
    grid-template-columns: 1fr;
    max-height: 46vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* 底部控制列:全寬、觸控友善(鈕≥44px、時間軸全寬) */
  .controls {
    left: 8px;
    right: 8px;
    bottom: 8px;
    transform: none;
    width: auto;
    grid-template-columns: 1fr;
    gap: 10px;
    padding: 12px;
  }
  .controls-left {
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
  }
  .controls-left button {
    flex: 1 1 28%;
    min-height: 44px;
    padding: 10px 8px;
    font-size: 14px;
  }
  .controls-left .date-display {
    flex: 1 1 100%;
    order: 9;
    text-align: center;
  }
  #date-slider { height: 26px; }
  .controls-right {
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 10px;
  }
  .controls-right .ghost-toggle {
    display: flex;
    align-items: center;
    min-height: 40px;
  }
  #speed-select { min-height: 40px; }

  /* 版權標示抬到控制列上方,避免相疊(--ctrl-h 由 index.html 量測控制列高度)*/
  .maplibregl-ctrl-bottom-right,
  .maplibregl-ctrl-bottom-left { bottom: calc(var(--ctrl-h, 200px) + 12px); }

  /* 系統選擇面板:改底部抽屜 sheet — 全寬、單欄、可捲動、大關閉鈕 */
  .sys-overlay { align-items: flex-end; }
  .sys-card {
    width: 100%;
    max-width: 100%;
    max-height: 88vh;
    border-radius: 16px 16px 0 0;
    padding: 14px 16px calc(16px + env(safe-area-inset-bottom, 0px));
  }
  .sys-head { font-size: 17px; margin-bottom: 12px; }
  .sys-head button {
    min-width: 44px;
    min-height: 44px;
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: -6px -8px -6px 0;
  }
  .sys-all { padding: 12px; font-size: 15px; margin-bottom: 12px; }
  .sys-item {
    display: flex;
    width: 100%;
    align-items: center;
    gap: 10px;
    padding: 11px 8px;
    font-size: 15px;
    border-radius: 8px;
  }
  .sys-item input { width: 20px; height: 20px; flex: 0 0 auto; }
  .sys-item i { font-size: 12px; }
  .sys-item:active { background: rgba(255, 255, 255, 0.06); }
  .sys-grp { margin: 14px 2px 2px; font-size: 12px; }
  .sys-actions {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    margin-top: 14px;
  }
  #sys-hint { order: 2; text-align: center; }
  #sys-apply {
    order: 1;
    width: 100%;
    min-height: 48px;
    padding: 12px;
    font-size: 16px;
  }
}
