:root {
  --brand: #2f7d5a;
  --brand-dark: #266549;
  --accent: #e8590c;
  --bg: #ffffff;
  --bg-soft: #f4f5f4;
  --text: #23302a;
  --text-dim: #6b7a72;
  --line: #dde3df;
  /* 現在地ボタン用の薄いブランド色（明暗どちらでも文字が読めるよう別に持つ） */
  --brand-soft: #e8f3ed;
  --brand-line: #b7d9c8;
  --brand-text: #1f6349;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.18);
  --radius: 18px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c221f;
    --bg-soft: #262e2a;
    --text: #e8ede9;
    --text-dim: #9caba2;
    --line: #39443e;
    --brand-soft: #23392f;
    --brand-line: #3c6b55;
    --brand-text: #7fd0a4;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  max-width: 100%;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic UI", "Noto Sans JP", sans-serif;
  color: var(--text);
  background: var(--bg);
  overscroll-behavior: none;
}

#map {
  position: fixed;
  inset: 0;
  z-index: 0;
  background: #dfe8e0;
}

/* ズームボタン・出典表記をトップバーの下へ */
.leaflet-top.leaflet-right .leaflet-control-zoom {
  margin-top: calc(64px + env(safe-area-inset-top));
}
.leaflet-top.leaflet-left .leaflet-control-attribution {
  margin-top: calc(58px + env(safe-area-inset-top));
  font-size: 10px;
  background: rgba(255, 255, 255, 0.75);
}

/* ===== トップバー ===== */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(8px + env(safe-area-inset-top)) 14px 8px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}

.brand {
  font-weight: 700;
  font-size: 17px;
  letter-spacing: 0.02em;
}

.icon-btn {
  border: none;
  background: none;
  font-size: 22px;
  padding: 4px 6px;
  cursor: pointer;
  line-height: 1;
}

/* ===== 現在地ボタン ===== */
/* スタート地点を決める一番手軽な手段なので、幅いっぱい＋ブランド色で目立たせる。
 * 「コースを引く」(塗り)とは差をつけて、薄い塗りにしてある。 */
.locate-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 42px;
  margin-bottom: 6px;
  border: 1px solid var(--brand-line);
  border-radius: 12px;
  background: var(--brand-soft);
  color: var(--brand-text);
  font-size: 14.5px;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
}
.locate-icon { font-size: 16px; line-height: 1; }
.locate-btn:active { background: var(--brand-line); }

/* ===== 散歩中の残り距離表示 ===== */
.walk-info {
  position: fixed;
  top: calc(64px + env(safe-area-inset-top) + 10px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  background: var(--brand);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  padding: 8px 16px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  white-space: nowrap;
}
.walk-info.hidden { display: none; }

/* 画面上部の通知。パネルの外なので畳んでいても必ず見える */
.toast {
  position: fixed;
  top: calc(64px + env(safe-area-inset-top) + 10px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 1002;
  width: max-content;
  max-width: calc(100vw - 28px);
  background: rgba(35, 48, 42, 0.94);
  color: #fff;
  font-size: 13.5px;
  font-weight: 600;
  line-height: 1.5;
  padding: 10px 16px;
  border-radius: 14px;
  box-shadow: var(--shadow);
  text-align: center;
}
.toast.error { background: rgba(180, 35, 35, 0.95); }
.toast.below { top: calc(64px + env(safe-area-inset-top) + 58px); } /* 残り距離表示と重ねない */
.toast.hidden { display: none; }

/* ===== ボトムシート ===== */
.sheet {
  position: fixed;
  left: 0; right: 0;
  /* iOS Safariの下部ツールバーに潜り込まないよう実測値ぶん持ち上げる */
  bottom: var(--vv-bottom, 0px);
  z-index: 1001;
  background: var(--bg);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: var(--shadow);
  max-height: 70vh;
  /* パネル上部の帯はどこを掴んでもドラッグできる（設定エリアだけ縦スクロールを許可） */
  touch-action: none;
  display: flex;
  flex-direction: column;
  transition: transform 0.25s ease;
}
/* 畳んだ時はハンドル＋結果（距離と操作ボタン）までを残す。高さはJSで実測 */
.sheet.collapsed {
  transform: translateY(calc(100% - var(--peek-h, 48px)));
}

.sheet-handle {
  border: none;
  background: none;
  padding: 14px 0 10px;   /* 指で掴みやすいよう縦に余裕を持たせる */
  cursor: grab;
  flex: none;
  -webkit-user-select: none;
  user-select: none;
}
.sheet-handle:active { cursor: grabbing; }

.sheet-handle .grip {
  display: block;
  width: 52px;
  height: 6px;
  margin: 0 auto;
  border-radius: 3px;
  background: var(--line);
}
.sheet-handle .peek {
  display: none;
  margin-top: 7px;
  font-size: 13px;
  line-height: 18px;   /* 畳んだ高さを確定させるため固定 */
  font-weight: 700;
  color: var(--text-dim);
}
/* 結果が出ている時はチップが情報を持つので、案内文は出さない */
.sheet.collapsed:not(.has-route) .sheet-handle .peek { display: block; }

.sheet-body {
  overflow-y: auto;
  touch-action: pan-y;
  padding: 0 18px calc(14px + env(safe-area-inset-bottom));
}

/* ===== 行・スライダー ===== */
.row { margin-bottom: 9px; }

.row-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 0;
  font-size: 13px;
}
.row-head label { color: var(--text-dim); }
.row-head.centered { justify-content: center; }
.row-head.centered label { font-weight: 700; font-size: 14px; color: var(--text); }
.row-val { font-weight: 700; font-size: 14px; }

/* スライダー両端のラベル（少ない / 多い） */
.range-ends {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-dim);
  margin-top: -4px;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--brand);
  margin: 2px 0 0;
}

.hint {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 1px;
}

.search-row {
  display: flex;
  gap: 6px;
  height: 42px;
}
.search-row input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  /* 16px未満だとiOS Safariがタップ時に自動ズームし、拡大が残って画面が見切れる */
  font-size: 16px;
  background: var(--bg-soft);
  color: var(--text);
}
#btnAddr {
  flex: none;
  width: 44px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-soft);
  font-size: 17px;
  cursor: pointer;
}

.addr-results {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: -4px 0 12px;
}
.addr-results.hidden { display: none; }
.addr-results button {
  text-align: left;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}
.addr-results button:active { background: var(--bg-soft); }

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  cursor: pointer;
}
.check-row input { width: 18px; height: 18px; accent-color: var(--brand); }

/* ===== ボタン ===== */
button.primary {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 14px;
  background: var(--brand);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  margin-top: 2px;
}
/* 押すたびに引き直せることが分かるよう、控えめに添える */
button.primary .sub {
  font-size: 12.5px;
  font-weight: 600;
  opacity: 0.82;
  margin-left: 3px;
}
button.primary:active { background: var(--brand-dark); }
button.primary:disabled { opacity: 0.55; }

/* 「コースを引く」は常に同じ場所・同じ見た目で出す（コースの有無で変えない）。
 * 畳んだ状態でも押せるようパネル上部に固定。 */
.primary-row {
  flex: none;
  padding: 0 18px 9px;
}

/* 並び順は状態で入れ替える。
 * 開いている時 : 設定（スタート地点→距離→坂道）→ コースを引く → 結果
 *               上から決めていって最後に引く、という順に読めるようにする
 * 畳んだ時     : コースを引く → 結果（見える範囲に距離と散歩開始を出す）→ 設定は画面外 */
.sheet-handle { order: 0; }
.sheet-body   { order: 1; }
.primary-row  { order: 2; }
.result       { order: 3; }

.sheet:not(.collapsed) .primary-row {
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.sheet.collapsed .primary-row { order: 1; }
.sheet.collapsed .result      { order: 2; }
.sheet.collapsed .sheet-body  { order: 3; }

/* ===== 結果（畳んでも見える帯） ===== */
.result {
  flex: none;
  padding: 10px 18px;
  border-top: 1px solid var(--line);   /* 開いている時は設定の下に置く */
}
.sheet.collapsed .result {
  padding: 0 18px 10px;
  border-top: none;
  border-bottom: 1px solid var(--line); /* 畳んだ時は上に来るので区切りを下へ */
}
.result.hidden { display: none; }

.chips {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}
.chip {
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 11px;
  font-size: 13px;
}
.chip b { font-size: 14px; }

.result-btns {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}
.result-btns button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 9px 4px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-soft);
  color: var(--text);
  cursor: pointer;
}
.result-btns button b { font-size: 13.5px; white-space: nowrap; }
.result-btns button small {
  font-size: 10px;
  font-weight: normal;
  color: var(--text-dim);
}
.result-btns button.active small { color: rgba(255, 255, 255, 0.85); }
.result-btns button.active {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

/* ===== ステータス ===== */
/* 通知は画面上部のトーストが担うので、ここは控えめに（空なら高さを取らない） */
.status {
  margin-top: 7px;
  font-size: 11.5px;
  line-height: 1.4;
  text-align: center;
  color: var(--text-dim);
}
.status:empty { display: none; }
.status.error { color: #c92a2a; }
.status.ok { color: var(--brand); }
.status.busy::after {
  content: "";
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-left: 6px;
  vertical-align: -2px;
  border: 2px solid var(--line);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== 設定ダイアログ ===== */
dialog {
  border: none;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  width: min(440px, calc(100vw - 32px));
  background: var(--bg);
  color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }

dialog h2 { margin: 0 0 10px; font-size: 18px; }
.dlg-note { font-size: 14px; margin: 0 0 8px; }
.dlg-steps { font-size: 13px; padding-left: 20px; margin: 0 0 12px; color: var(--text-dim); }
.dlg-steps li { margin-bottom: 4px; }
.dlg-steps a { color: var(--brand); }

.dlg-label { font-size: 13px; color: var(--text-dim); }
#apiKey {
  width: 100%;
  padding: 10px;
  margin-top: 4px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 14px;
  background: var(--bg-soft);
  color: var(--text);
}

.key-status { min-height: 20px; font-size: 13px; margin: 6px 0; }

.dlg-sub {
  font-size: 14px;
  margin: 16px 0 8px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}
.diag-btn {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-soft);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.diag-out {
  margin: 10px 0 0;
  padding: 10px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 11px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 200px;
  overflow-y: auto;
  -webkit-user-select: all;
  user-select: all;
}
.diag-out.hidden { display: none; }

.dlg-btns { display: flex; gap: 8px; }
.dlg-btns button {
  flex: 1;
  padding: 11px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.dlg-btns button.primary { border: none; margin-top: 0; font-size: 14px; }

.dlg-fine { font-size: 11px; color: var(--text-dim); margin: 10px 0 0; }
