:root {
  --brand: #1f6feb;
  --brand-soft: rgba(31, 111, 235, 0.15);
  --surface: #ffffff;
  --text: #1b1f24;
  --text-muted: #5b6573;
  --danger: #d1242f;
  --border: #e3e8ee;
  --hover: #f3f6f9;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface: #1d232b;
    --text: #e8edf3;
    --text-muted: #9aa6b4;
    --border: #2f3843;
    --hover: #262e38;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

/* display 를 지정한 요소에서도 hidden 속성이 항상 이기도록 */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  background: #e9eef3;
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

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

/* 상태 메시지 */
.status {
  position: absolute;
  top: calc(76px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  font-size: 14px;
  line-height: 1.4;
  z-index: 10;
}
.status.is-error { border-left: 4px solid var(--danger); }

/* 우측 하단 버튼 */
.fab-stack {
  position: absolute;
  right: 16px;
  bottom: calc(32px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 10;
}
.fab {
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 50%;
  background: var(--surface);
  color: var(--text-muted);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  cursor: pointer;
}
.fab:active { transform: scale(0.95); }
.fab.is-active { color: var(--brand); }

/* 내 위치 마커 */
.me-marker {
  width: 22px;
  height: 22px;
  position: relative;
}
.me-marker .dot {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: var(--brand);
  border: 3px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15), 0 1px 4px rgba(0, 0, 0, 0.3);
}
/* 진행 방향이 있으면 위쪽에 삼각형을 붙인다. 마커 자체를 heading 만큼 회전시킨다 */
.me-marker .arrow {
  display: none;
  position: absolute;
  left: 50%;
  top: -11px;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-bottom: 10px solid var(--brand);
}
.me-marker.has-heading .arrow { display: block; }

/* 공통 버튼 */
.icon-btn {
  width: 36px;
  height: 36px;
  flex: none;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  display: grid;
  place-items: center;
  cursor: pointer;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn[hidden] { display: none; }

.primary-btn {
  width: 100%;
  height: 48px;
  border: 0;
  border-radius: 10px;
  background: var(--brand);
  color: #fff;
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.primary-btn:active { filter: brightness(0.92); }
.primary-btn:disabled { opacity: 0.5; cursor: default; }

/* 상단 검색 */
.search {
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: 16px;
  right: 16px;
  max-width: 480px;
  z-index: 20;
}
.search-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 52px;
  padding: 0 6px 0 14px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.search-icon { flex: none; color: var(--text-muted); }
.search-bar input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 16px; /* 16px 미만이면 iOS 가 입력 시 확대한다 */
  padding-left: 6px;
}
.search-bar input::-webkit-search-cancel-button { display: none; }

.search-results {
  list-style: none;
  margin: 8px 0 0;
  padding: 4px 0;
  max-height: min(60vh, 480px);
  overflow-y: auto;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.search-results li + li { border-top: 1px solid var(--border); }
.place-item { display: flex; align-items: center; }
.place-item > button:first-child {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.place-item > button:first-child:hover,
.place-item > button:first-child:focus-visible { background: var(--hover); outline: none; }
.place-body { flex: 1; min-width: 0; display: block; }
.place-icon { flex: none; display: grid; place-items: center; color: var(--text-muted); }
.place-item.is-favorite .place-icon { color: #f59e0b; }
.saved-remove { margin-right: 6px; }
.saved-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px 4px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
}
.search-results li.saved-header + li { border-top: 0; }
.saved-header .link-btn { font-size: 12px; padding: 0; }
.place-main { display: flex; align-items: baseline; gap: 8px; }
.place-name { font-size: 16px; font-weight: 600; }
.place-category { font-size: 12px; color: var(--text-muted); flex: none; }
.place-sub {
  display: flex;
  gap: 8px;
  margin-top: 3px;
  font-size: 13px;
  color: var(--text-muted);
}
.place-distance { flex: none; color: var(--brand); font-weight: 600; }
.place-address { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search-message {
  padding: 14px 16px;
  font-size: 14px;
  color: var(--text-muted);
}
.search-message.is-error { color: var(--danger); }

/* 하단 시트 (장소 카드) */
.sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-width: 560px;
  margin: 0 auto;
  padding: 20px 16px calc(16px + var(--safe-bottom));
  border-radius: 18px 18px 0 0;
  background: var(--surface);
  box-shadow: 0 -2px 16px rgba(0, 0, 0, 0.18);
  z-index: 15;
}
.sheet-close { position: absolute; top: 10px; right: 10px; }
.sheet-fav { position: absolute; top: 10px; right: 50px; }
.sheet-fav[aria-pressed="true"] { color: #f59e0b; }
.sheet-fav .icon-star-on { display: none; }
.sheet-fav[aria-pressed="true"] .icon-star-on { display: block; }
.sheet-fav[aria-pressed="true"] .icon-star-off { display: none; }
.card-title {
  margin: 0 84px 4px 0;
  font-size: 20px;
  line-height: 1.3;
}
.card-meta { margin: 0 0 4px; font-size: 14px; color: var(--brand); font-weight: 600; }
.card-meta[hidden] { display: none; }
.card-address { margin: 0 0 16px; font-size: 14px; color: var(--text-muted); }

.sheet-header {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: -8px -6px 8px;
}
.sheet-header h2 {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 17px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.route-summary { display: flex; align-items: baseline; gap: 10px; margin-bottom: 10px; }
.route-summary[hidden] { display: none; }
.route-duration { font-size: 28px; font-weight: 700; color: var(--brand); }
.route-meta { font-size: 15px; color: var(--text-muted); }
.route-message { margin: 4px 0 12px; font-size: 15px; color: var(--text-muted); }
.route-message.is-error { color: var(--danger); }

.route-options {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--brand); }
.link-btn {
  border: 0;
  background: none;
  padding: 6px 0;
  color: var(--brand);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.link-btn[hidden] { display: none; }

.route-steps {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
  max-height: 34vh;
  overflow-y: auto;
  border-top: 1px solid var(--border);
}
.route-steps li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 2px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.step-icon { flex: none; display: grid; place-items: center; color: var(--brand); }
.step-text { flex: 1; min-width: 0; }
.step-distance { flex: none; color: var(--text-muted); font-size: 13px; }

.sheet-actions { display: flex; gap: 8px; }
.secondary-btn {
  flex: none;
  height: 48px;
  padding: 0 18px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 15px;
  cursor: pointer;
}
.secondary-btn[hidden] { display: none; }

/* 시트가 열리면 우측 버튼과 OSM 저작권 표시를 시트 위로 올린다.
   --sheet-h 는 main.js 가 시트 높이를 재서 넣는다 */
body.has-sheet .fab-stack { bottom: calc(var(--sheet-h, 200px) + 16px); }
body.has-sheet .maplibregl-ctrl-bottom-right { bottom: var(--sheet-h, 184px); }

/* ---------- 주행 화면 ---------- */
.hud { position: absolute; inset: 0; pointer-events: none; z-index: 25; }
.hud > * { pointer-events: auto; }

.hud-top {
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: 12px;
  right: 12px;
  max-width: 520px;
}
.hud-banner {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  border-radius: 14px;
  background: #14532d;
  color: #fff;
  box-shadow: var(--shadow);
}
.hud-banner.is-off-route { background: #9a3412; }
.hud-banner.is-arrived { background: #1e3a8a; }
.hud-icon { flex: none; width: 44px; height: 44px; display: grid; place-items: center; }
.hud-main { min-width: 0; }
.hud-distance { font-size: 30px; font-weight: 800; line-height: 1.1; letter-spacing: -0.5px; }
.hud-distance:empty { display: none; }
.hud-instruction {
  font-size: 17px;
  font-weight: 600;
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.hud-then {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  padding: 6px 12px;
  border-radius: 10px;
  background: #166534;
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  box-shadow: var(--shadow);
}

.hud-speed {
  position: absolute;
  left: 16px;
  bottom: calc(96px + var(--safe-bottom));
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.hud-speed-value { font-size: 22px; font-weight: 800; }
.hud-speed small { font-size: 10px; color: var(--text-muted); margin-top: 2px; }

.hud-bottom {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px calc(14px + var(--safe-bottom));
  background: var(--surface);
  box-shadow: 0 -2px 16px rgba(0, 0, 0, 0.18);
}
.hud-stats { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.hud-eta { font-size: 22px; font-weight: 800; color: var(--text); }
.hud-remaining { font-size: 14px; color: var(--text-muted); }
.hud-end {
  flex: none;
  height: 44px;
  padding: 0 20px;
  border: 0;
  border-radius: 10px;
  background: var(--danger);
  color: #fff;
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}

.hud-fav {
  flex: none;
  height: 44px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-muted);
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}
.hud-fav[aria-pressed="true"] { color: #f59e0b; border-color: #f59e0b; }

.sim-controls {
  position: absolute;
  right: 12px;
  top: calc(130px + var(--safe-top));
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px;
  border-radius: 10px;
  background: rgba(17, 24, 39, 0.85);
  color: #fff;
  font-size: 12px;
}
.sim-controls span { margin-right: 4px; opacity: 0.8; }
.sim-controls button {
  min-width: 36px;
  height: 30px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 6px;
  background: transparent;
  color: #fff;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}
.sim-controls button[aria-pressed="true"] { background: #fff; color: #111827; }

.sim-controls [data-action="detour"] { margin-left: 4px; border-color: #fb923c; color: #fdba74; }
.sim-controls [data-action="detour"]:disabled { opacity: 0.4; cursor: default; }

/* 음소거 버튼: 주행 중에만 보인다 */
.drive-only { display: none; }
body.is-driving .drive-only { display: grid; }
#btn-mute .icon-muted { display: none; }
#btn-mute[aria-pressed="true"] { color: var(--danger); }
#btn-mute[aria-pressed="true"] .icon-sound { display: none; }
#btn-mute[aria-pressed="true"] .icon-muted { display: block; }

/* 주행 중에는 일반 화면 요소를 숨기고, 현위치 버튼은 하단 바 위로 */
body.is-driving .search,
body.is-driving .sheet { display: none; }
body.is-driving .fab-stack { bottom: calc(96px + var(--safe-bottom)); z-index: 30; }
body.is-driving .maplibregl-ctrl-bottom-right { bottom: calc(80px + var(--safe-bottom)); }
body.is-driving .status { top: calc(180px + var(--safe-top)); z-index: 30; }

/* 저작권 표시가 버튼에 가리지 않도록 */
.maplibregl-ctrl-bottom-right { margin-right: 72px; }

/* ---------- 즐겨찾기: 바로가기 줄 ---------- */
:root { --home: #16a34a; --work: #7c3aed; --star: #f59e0b; }
@media (prefers-color-scheme: dark) {
  :root { --home: #4ade80; --work: #a78bfa; --star: #fbbf24; }
}

.quick-bar {
  display: flex;
  gap: 8px;
  margin: 8px -16px 0;
  padding: 2px 16px 6px;
  overflow-x: auto;
  scrollbar-width: none;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
}
.quick-bar::-webkit-scrollbar { display: none; }
/* 검색 결과가 열려 있거나 하단 시트가 있으면 숨긴다 */
.search-results:not([hidden]) + .quick-bar,
body.has-sheet .quick-bar,
body.is-driving .quick-bar { display: none; }

.chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  max-width: 180px;
  padding: 0 14px 0 10px;
  border: 0;
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.16);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.chip:active { transform: scale(0.97); }
.chip:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.chip-icon { display: grid; place-items: center; color: var(--text-muted); }
.chip-label { overflow: hidden; text-overflow: ellipsis; }
.chip.is-home .chip-icon { color: var(--home); }
.chip.is-work .chip-icon { color: var(--work); }
.chip.is-favorite .chip-icon { color: var(--star); }
.chip.is-empty { color: var(--text-muted); font-weight: 500; }
.chip.is-manage { padding: 0 12px 0 10px; color: var(--text-muted); }

/* 검색 목록의 집·회사 아이콘 */
.place-item.is-home .place-icon { color: var(--home); }
.place-item.is-work .place-icon { color: var(--work); }

/* ---------- 장소 카드: 집·회사로 등록 ---------- */
.card-slots { display: flex; gap: 8px; margin: -4px 0 14px; flex-wrap: wrap; }
.slot-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 17px;
  background: var(--surface);
  color: var(--text-muted);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.slot-btn:hover { background: var(--hover); }
.slot-btn[data-slot="home"][aria-pressed="true"] { color: var(--home); border-color: currentColor; }
.slot-btn[data-slot="work"][aria-pressed="true"] { color: var(--work); border-color: currentColor; }

/* ---------- 즐겨찾기 관리 화면 ---------- */
.panel {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  color: var(--text);
}
@media (min-width: 600px) {
  .panel {
    inset: calc(12px + var(--safe-top)) auto 12px 16px;
    width: 400px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
  }
}
.panel-header {
  flex: none;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(8px + var(--safe-top)) 8px 8px;
  border-bottom: 1px solid var(--border);
}
.panel-header h2 { margin: 0; font-size: 18px; }
.panel-body { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 0 12px; }
.panel-section {
  margin: 0;
  padding: 16px 16px 6px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
}
.panel-count { font-weight: 600; color: var(--brand); margin-left: 2px; }
.panel-empty { margin: 8px 16px; font-size: 14px; line-height: 1.6; color: var(--text-muted); }
.panel-footer {
  flex: none;
  padding: 12px 16px calc(12px + var(--safe-bottom));
  border-top: 1px solid var(--border);
}
.panel-add {
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-weight: 600;
}
.panel-hint { margin: 8px 0 0; font-size: 12px; line-height: 1.5; color: var(--text-muted); text-align: center; }

.manage-list { list-style: none; margin: 0; padding: 0; }
.manage-item {
  display: flex;
  align-items: center;
  gap: 2px;
  padding-right: 8px;
  border-bottom: 1px solid var(--border);
}
.manage-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 8px 12px 16px;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.manage-main:hover, .manage-main:focus-visible { background: var(--hover); outline: none; }
.manage-icon { flex: none; display: grid; place-items: center; color: var(--text-muted); }
.manage-item.is-home .manage-icon { color: var(--home); }
.manage-item.is-work .manage-icon { color: var(--work); }
.manage-item.is-favorite .manage-icon,
.manage-item.is-editing .manage-icon { color: var(--star); }
.manage-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.manage-name { font-size: 16px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.manage-sub { font-size: 13px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.manage-item.is-empty .manage-name { color: var(--text-muted); font-weight: 500; }
.manage-plus { flex: none; display: grid; place-items: center; width: 36px; color: var(--brand); }
.manage-actions { flex: none; display: flex; }
.manage-actions .icon-btn { width: 34px; height: 34px; }
.icon-btn:disabled { opacity: 0.3; cursor: default; background: none; }

.manage-edit {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0 8px 16px;
}
.manage-edit input {
  flex: 1;
  min-width: 0;
  height: 40px;
  padding: 0 10px;
  border: 1px solid var(--brand);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px;
  outline: none;
}
.text-btn {
  flex: none;
  height: 34px;
  padding: 0 8px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--text-muted);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.text-btn:hover { background: var(--hover); }
.text-btn.is-primary { color: var(--brand); }

/* 바로가기 줄이 보일 때는 상태 메시지를 그 아래로 내린다 */
body:not(.has-sheet):not(.is-driving) #search:not([hidden]) ~ .status { top: calc(124px + var(--safe-top)); }

/* 배경 지도 전환 버튼 (일반 ↔ 항공) */
.fab-layer { display: flex; flex-direction: column; gap: 1px; }
.fab-label { font-size: 10px; font-weight: 700; line-height: 1; }
.fab-layer[aria-pressed="true"] { color: var(--brand); }
/* 항공사진 위에서는 OSM 저작권 표시 글자가 잘 보이게 */
body.is-aerial .maplibregl-ctrl-attrib { background: rgba(255, 255, 255, 0.85); }
