:root {
  --color-bg: #f5f3ee;
  --color-surface: #ffffff;
  --color-text: #1c1c1c;
  --color-text-sub: #6b6b6b;
  --color-primary: #1b6b4a;
  --color-primary-dark: #124f37;
  --color-border: #e0ddd5;
  --color-chip-bg: #eeece5;
  --color-chip-active-bg: #1b6b4a;
  --color-chip-active-text: #ffffff;
  --color-caution-bg: #fff4e5;
  --color-caution-border: #e8a33d;
  --color-danger-text: #8a4b00;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #14161a;
    --color-surface: #1e2126;
    --color-text: #ececec;
    --color-text-sub: #9a9a9a;
    --color-primary: #3ea679;
    --color-primary-dark: #2c7d5b;
    --color-border: #30343b;
    --color-chip-bg: #2a2d33;
    --color-chip-active-bg: #3ea679;
    --color-chip-active-text: #0d120f;
    --color-caution-bg: #33280f;
    --color-caution-border: #c98a2c;
    --color-danger-text: #f0b968;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

.hidden { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", sans-serif;
  color: var(--color-text);
  background: var(--color-bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#app-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) calc(12px + var(--safe-right)) 10px calc(12px + var(--safe-left));
  background: var(--color-primary);
  color: #fff;
  z-index: 20;
}

#app-header h1 {
  font-size: 16px;
  margin: 0;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#locate-btn {
  flex: 0 0 auto;
  min-width: 44px;
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid rgba(255,255,255,0.6);
  border-radius: 10px;
  background: rgba(255,255,255,0.12);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
}

#locate-btn.active {
  background: #fff;
  color: var(--color-primary-dark);
}

#app-main {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
}

#map {
  position: absolute;
  inset: 0;
  z-index: 1;
  background: var(--color-chip-bg);
}

#map :is(.leaflet-control-attribution) {
  font-size: 10px;
}

/* bottom sheet panel */
#panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--color-surface);
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.18);
  display: flex;
  flex-direction: column;
  max-height: 78%;
  transition: transform 0.25s ease;
  padding-bottom: var(--safe-bottom);
  z-index: 15;
}

#panel.panel-collapsed {
  transform: translateY(calc(100% - 56px - var(--safe-bottom)));
}

#panel-handle {
  flex: 0 0 auto;
  width: 100%;
  min-height: 56px;
  background: transparent;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  color: var(--color-text);
  font-size: 14px;
  font-weight: 700;
}

.handle-bar {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--color-border);
}

#panel-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0 14px 20px;
}

#filters {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 4px 0 14px;
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.filter-label {
  font-size: 12px;
  color: var(--color-text-sub);
  font-weight: 700;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  min-height: 36px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--color-border);
  background: var(--color-chip-bg);
  color: var(--color-text);
  font-size: 13px;
  font-weight: 600;
}

.chip.active {
  background: var(--color-chip-active-bg);
  color: var(--color-chip-active-text);
  border-color: transparent;
}

#course-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.course-card {
  width: 100%;
  text-align: left;
  border: 1px solid var(--color-border);
  border-radius: 12px;
  background: var(--color-surface);
  padding: 12px 14px;
  min-height: 44px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.course-card-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.course-card-name {
  font-size: 15px;
  font-weight: 700;
  line-height: 1.4;
}

.badge {
  flex: 0 0 auto;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  white-space: nowrap;
}
.badge.diff-初級 { background: #e2f3ea; color: #1b6b4a; }
.badge.diff-中級 { background: #fff0d9; color: #a1650a; }
.badge.diff-上級 { background: #fde3e1; color: #b3382c; }

@media (prefers-color-scheme: dark) {
  .badge.diff-初級 { background: #163a2a; color: #6cd6a2; }
  .badge.diff-中級 { background: #3a2c10; color: #f0b968; }
  .badge.diff-上級 { background: #3d1c18; color: #f19086; }
}

.course-card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  font-size: 12px;
  color: var(--color-text-sub);
}

.course-card-area {
  font-size: 12px;
  color: var(--color-text-sub);
}

#empty-state {
  padding: 30px 10px;
  text-align: center;
  color: var(--color-text-sub);
  font-size: 14px;
}

/* detail view */
#detail-view {
  position: absolute;
  inset: 0;
  background: var(--color-surface);
  z-index: 25;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: calc(14px + var(--safe-top)) calc(16px + var(--safe-right)) calc(24px + var(--safe-bottom)) calc(16px + var(--safe-left));
}

#detail-back {
  min-height: 44px;
  padding: 0 10px 0 0;
  border: none;
  background: transparent;
  color: var(--color-primary);
  font-size: 15px;
  font-weight: 700;
}

#detail-name {
  font-size: 20px;
  margin: 10px 0 2px;
  line-height: 1.4;
}

#detail-area {
  margin: 0 0 14px;
  color: var(--color-text-sub);
  font-size: 13px;
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}

.stat-box {
  background: var(--color-chip-bg);
  border-radius: 12px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-label {
  font-size: 11px;
  color: var(--color-text-sub);
}

.stat-value {
  font-size: 18px;
  font-weight: 700;
}

#elevation-canvas {
  width: 100%;
  height: 110px;
  display: block;
  margin-bottom: 6px;
  border-radius: 12px;
  background: var(--color-chip-bg);
}

#elevation-view-note {
  font-size: 11px;
  color: var(--color-text-sub);
  margin: 0 0 16px;
}

.detail-block {
  margin-bottom: 18px;
}

.detail-block h3 {
  font-size: 14px;
  margin: 0 0 8px;
}

.detail-block ul {
  margin: 0;
  padding-left: 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 14px;
  line-height: 1.5;
}

#detail-cautions {
  background: var(--color-caution-bg);
  border: 1px solid var(--color-caution-border);
  border-radius: 12px;
  padding: 12px 14px;
}

#detail-cautions h3 {
  color: var(--color-danger-text);
}

#detail-cautions ul {
  color: var(--color-danger-text);
}

.primary-btn {
  width: 100%;
  min-height: 48px;
  border: none;
  border-radius: 12px;
  background: var(--color-primary);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
}

.external-map-links {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 10px;
}

.secondary-btn {
  width: 100%;
  min-height: 48px;
  box-sizing: border-box;
  border: 1px solid var(--color-primary);
  border-radius: 12px;
  background: transparent;
  color: var(--color-primary);
  font-size: 15px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}

.external-map-note {
  font-size: 11px;
  color: var(--color-text-sub);
  margin: 8px 0 0;
  text-align: center;
}

#toast {
  position: absolute;
  left: 50%;
  bottom: calc(24px + var(--safe-bottom));
  transform: translateX(-50%);
  background: rgba(20,20,20,0.9);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 40;
  max-width: 90%;
  text-align: center;
}

.marker-pin {
  width: 28px;
  height: 28px;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 12px;
  font-weight: 800;
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.4);
}
.marker-pin::before {
  content: attr(data-label);
  transform: rotate(45deg);
}
.marker-start { background: #1b6b4a; }
.marker-end { background: #c9432f; }

/* leaflet marker popups on dark mode */
@media (prefers-color-scheme: dark) {
  .leaflet-popup-content-wrapper, .leaflet-popup-tip {
    background: var(--color-surface);
    color: var(--color-text);
  }
}
