:root {
  --bg: #eef1f4;
  --panel: #ffffff;
  --border: #d4dae1;
  --text: #1f2a35;
  --muted: #6b7785;
  --accent: #17607f;
  --accent-soft: #e2eff5;
  --gap: 8px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
  /* dvh 會隨手機瀏覽器網址列伸縮調整，避免底部留白或被遮住 */
  height: 100dvh;
  display: grid;
  grid-template-columns: 260px 1fr;
  grid-template-rows: 1fr 240px;
  grid-template-areas:
    "side map"
    "side profile";
  gap: var(--gap);
  padding: var(--gap);
  padding:
    max(var(--gap), env(safe-area-inset-top)) max(var(--gap), env(safe-area-inset-right))
    max(var(--gap), env(safe-area-inset-bottom)) max(var(--gap), env(safe-area-inset-left));
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Microsoft JhengHei", "PingFang TC", sans-serif;
  font-size: 14px;
}

#sidebar, #map-area, #profile-area {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  min-width: 0;
  min-height: 0;
}

/* ---- 區域 1 ---- */
#sidebar { grid-area: side; display: flex; flex-direction: column; }

#sidebar header {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
}
#sidebar h1 { font-size: 16px; margin: 0; }
#sidebar .actions { display: flex; gap: 6px; }
#sidebar button, #file-list .message button {
  border: 1px solid var(--border); background: var(--panel); border-radius: 6px;
  height: 28px; padding: 0 8px; cursor: pointer; font: inherit; font-size: 13px; color: var(--text);
}
#sidebar button:hover { color: var(--accent); border-color: var(--accent); }
#reload { width: 28px; padding: 0; color: var(--muted); }
#sidebar #list-toggle { display: none; }
#folder-name { width: 100%; color: var(--muted); font-size: 12px; word-break: break-all; }
#folder-name:not(:empty)::before { content: "📁 "; }
#file-list .message { cursor: default; color: var(--muted); line-height: 1.6; }
#file-list .message:hover { background: none; }
#file-list .message button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }

#file-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
#file-list li {
  padding: 9px 10px; border-radius: 7px; cursor: pointer; margin-bottom: 2px;
  border-left: 3px solid transparent;
}
#file-list li:hover { background: #f4f6f8; }
#file-list li.active { background: var(--accent-soft); border-left-color: var(--accent); }
#file-list .name { font-weight: 600; word-break: break-all; }
#file-list .meta { color: var(--muted); font-size: 12px; margin-top: 3px; }
#file-list .error { color: #b3261e; }

/* ---- 區域 2 ---- */
#map-area { grid-area: map; position: relative; }
#map { position: absolute; inset: 0; }

#track-info {
  position: absolute; left: 54px; top: 10px; z-index: 500;
  background: rgba(255, 255, 255, 0.93); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 12px; box-shadow: 0 1px 4px rgba(0,0,0,.15);
  max-width: calc(100% - 260px);
}
#track-info h2 { margin: 0 0 4px; font-size: 15px; }
#track-info .stats { display: flex; flex-wrap: wrap; gap: 4px 14px; color: var(--muted); font-size: 12.5px; }
#track-info .stats b { color: var(--text); }

.grade-toggle {
  background: #fff; padding: 6px 10px; border-radius: 6px; font-size: 13px;
  box-shadow: 0 1px 5px rgba(0,0,0,.4); cursor: pointer; user-select: none;
}
.grade-toggle input { vertical-align: middle; margin: 0 4px 0 0; }
.grade-toggle .short { display: none; }

.pin {
  width: 14px; height: 14px; border-radius: 50%; border: 2px solid #fff;
  box-shadow: 0 0 3px rgba(0,0,0,.6);
}
.pin.start { background: #1e9e3a; }
.pin.end { background: #d62f2f; }

/* ---- 區域 3 ---- */
#profile-area { grid-area: profile; position: relative; }
/* touch-action: none 讓手指在剖面圖上滑動時查看數值，而不是捲動頁面 */
#profile { display: block; width: 100%; height: 100%; touch-action: none; }
#profile-empty {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--muted);
}
#profile-tip {
  position: absolute; pointer-events: none; z-index: 2;
  background: rgba(31, 42, 53, 0.92); color: #fff; font-size: 12px; line-height: 1.5;
  padding: 5px 9px; border-radius: 6px; white-space: nowrap;
}

#drop-overlay {
  position: fixed; inset: 0; z-index: 2000; display: flex; align-items: center; justify-content: center;
  background: rgba(23, 96, 127, 0.25); pointer-events: none;
}
#drop-overlay div {
  background: var(--panel); border: 2px dashed var(--accent); border-radius: 12px;
  padding: 24px 36px; font-size: 18px; color: var(--accent);
}

/* ---- 手機／小視窗：精簡版面（直式與橫式共用） ---- */
@media (max-width: 720px), (max-height: 500px) {
  body { --gap: 6px; }

  /* 側欄縮成一列標題，路線清單改為點選後展開、覆蓋在地圖上的下拉選單 */
  #sidebar { position: relative; z-index: 1100; overflow: visible; }
  #sidebar header { flex-wrap: nowrap; padding: 5px; border-bottom: 0; }
  #sidebar h1, #folder-name { display: none; }
  #sidebar #list-toggle {
    display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0;
    height: 38px; padding: 0 10px; border: 0; font-size: 15px; font-weight: 600; text-align: left;
  }
  #list-toggle .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #list-toggle .caret { color: var(--muted); transition: transform .15s; }
  body.list-open #list-toggle .caret { transform: rotate(180deg); }

  #file-list {
    display: none; position: absolute; top: calc(100% + 6px); left: 0; right: 0;
    max-height: min(65dvh, calc(100dvh - 80px));
    background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
    box-shadow: 0 6px 20px rgba(0,0,0,.2);
  }
  body.list-open #file-list { display: block; }

  /* 路線資訊只留一行主要統計，名稱已顯示在標題列 */
  #track-info { left: 50px; top: 8px; padding: 5px 9px; max-width: calc(100% - 104px); }
  #track-info h2, #track-info .extra { display: none; }
  #track-info .stats { font-size: 12px; gap: 2px 10px; }

  .grade-toggle { padding: 5px 8px; font-size: 12px; }
  .grade-toggle .full { display: none; }
  .grade-toggle .short { display: inline; }
}

/* 直式：標題列 / 地圖 / 剖面圖 */
@media (max-width: 720px) {
  body {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr minmax(170px, 32dvh);
    grid-template-areas: "side" "map" "profile";
  }
}

/* 橫式（高度很小）：左側標題列 + 地圖，右側剖面圖 */
@media (max-height: 500px) and (orientation: landscape) {
  body {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto 1fr;
    grid-template-areas: "side profile" "map profile";
  }
}

/* 觸控裝置無法選擇本機資料夾，隱藏相關按鈕 */
@media (pointer: coarse) {
  #sidebar .actions, #folder-name { display: none; }
}
