/* =====================================================================
   blunod — 컴포넌트 & 레이아웃
   토큰은 tokens.css (shared/tokens.json 에서 생성). 여기에 색 리터럴 금지.
   ===================================================================== */

*, *::before, *::after { box-sizing: border-box; }

/* peek 상태에서 시트가 보이는 높이. 지도 컨트롤·상태 알약·토스트가 이 값에 맞춰 뜬다. */
:root { --sheet-peek: 116px; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg-base);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.4;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
a { color: inherit; text-decoration: none; }

:where(a, button, input, [tabindex]):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}

.ic { display: block; flex: none; }

/* 시각적으로만 숨김 — 스크린리더에는 남는다 */
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}

.num { font-family: var(--font-display); font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- shell */
.app {
  position: relative;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ---------------------------------------------------------------- header */
.header {
  position: relative;
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: var(--sp-s3);
  padding: max(56px, calc(env(safe-area-inset-top) + 16px)) var(--sp-gutter) 14px;
  background: var(--bg-base);
  border-bottom: 1px solid var(--line);
}

.header__top { display: flex; align-items: center; gap: var(--sp-s3); }

.wordmark {
  font-family: var(--font-display);
  font-size: 20px; font-weight: 700;
  letter-spacing: -0.01em;
  margin-right: auto;
}
.wordmark span { color: var(--accent); }

/* ---------------------------------------------------------------- search */
.search { position: relative; display: flex; align-items: center; }
.search > .ic {
  position: absolute; left: 15px; color: var(--text-dim); pointer-events: none;
}
.search input {
  width: 100%;
  height: var(--sz-searchField);
  padding: 0 14px 0 44px;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--r-card);
  color: var(--text);
}
.search input::placeholder { color: var(--text-dim); }
.search input:focus { border-color: var(--accent); }
.search__clear {
  position: absolute; right: 6px;
  width: 36px; height: 36px;
  display: grid; place-items: center;
  color: var(--text-dim); border-radius: 10px;
}

/* ---------------------------------------------------------------- chips */
.chips {
  display: flex; gap: var(--sp-s2);
  overflow-x: auto;
  scrollbar-width: none;
  margin: 0 calc(var(--sp-gutter) * -1);
  padding: 0 var(--sp-gutter);
}
.chips::-webkit-scrollbar { display: none; }

.chip {
  display: inline-flex; align-items: center; gap: 7px;
  flex: none;
  height: var(--sz-chip);
  padding: 0 14px;
  border-radius: var(--r-chip);
  background: var(--bg-card);
  border: 1px solid var(--line);
  font-family: var(--font-display);
  font-size: 13px; font-weight: 600;
  white-space: nowrap;
  transition: background var(--motion-fast), border-color var(--motion-fast);
}
.chip[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.chip[aria-pressed="true"] .ic { color: inherit; }
.chip--weigh  .ic { color: var(--cat-weigh); }
.chip--border .ic { color: var(--cat-border); }
.chip--port   .ic { color: var(--cat-port); }
.chip--all    .ic { color: var(--accent); }

/* ---------------------------------------------------------------- buttons */
.icon-btn {
  width: var(--sz-iconBtnHeader); height: var(--sz-iconBtnHeader);
  display: grid; place-items: center;
  flex: none;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--r-card);
  color: var(--text);
  transition: border-color var(--motion-fast);
}
.icon-btn:hover { border-color: var(--line-strong); }
.icon-btn--map { width: var(--sz-iconBtnMap); height: var(--sz-iconBtnMap); border-radius: 15px; }
.icon-btn.is-busy .ic { animation: spin 900ms linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.btn-primary {
  display: flex; align-items: center; justify-content: center; gap: 9px;
  width: 100%; height: var(--sz-btnPrimary);
  border-radius: var(--r-button);
  background: var(--accent); color: var(--accent-ink);
  font-family: var(--font-display); font-size: 16px; font-weight: 700;
}
.btn-secondary {
  display: flex; align-items: center; justify-content: center; gap: 9px;
  width: 100%; height: var(--sz-btnSecondary);
  border-radius: var(--r-button);
  background: transparent;
  border: 1px solid var(--line-strong);
  font-family: var(--font-display); font-size: 15px; font-weight: 600;
}
.btn-quiet {
  display: inline-flex; align-items: center; gap: 6px;
  height: var(--sz-chip); padding: 0 12px;
  border-radius: var(--r-chip);
  background: var(--bg-card); border: 1px solid var(--line);
  font-family: var(--font-display); font-size: 13px; font-weight: 600;
}

/* ---------------------------------------------------------------- badges */
.badge {
  display: grid; place-items: center;
  flex: none;
  width: var(--sz-badgeList); height: var(--sz-badgeList);
  border-radius: var(--r-badge);
}
.badge--lg { width: var(--sz-badgeDetail); height: var(--sz-badgeDetail); border-radius: var(--r-card); }
.badge--sm { width: var(--sz-badgeMarker); height: var(--sz-badgeMarker); border-radius: 9px; }

.badge[data-cat="WEIGH_STATION"]   { background: var(--cat-weigh-bg);  color: var(--cat-weigh); }
.badge[data-cat="BORDER_CROSSING"] { background: var(--cat-border-bg); color: var(--cat-border); }
.badge[data-cat="PORT_GATE"]       { background: var(--cat-port-bg);   color: var(--cat-port); }

/* ---------------------------------------------------------------- map */
.map-wrap { position: relative; flex: 1 1 auto; min-height: 0; overflow: hidden; background: var(--bg-map); }
#map { position: absolute; inset: 0; }
#map canvas:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* 지도 → 시트 연결 그라디언트. 시트 위 160px 구간. */
.map-fade {
  position: absolute; left: 0; right: 0; bottom: 0; height: 160px;
  background: linear-gradient(to bottom, transparent, var(--scrim));
  pointer-events: none; z-index: 5;
  opacity: 0; transition: opacity var(--motion-sheet);
}
.app[data-detent="mid"] .map-fade,
.app[data-detent="detail"] .map-fade,
.app[data-detent="full"] .map-fade { opacity: 1; }

/* 마커 — 라벨 알약형. 줌이 낮으면 .marker--compact 로 배지만 남는다. */
/* 위치는 MapLibre Marker 가 transform 으로 잡는다. 여기서 transform 을 쓰면 덮어쓴다. */
.marker {
  display: inline-flex; align-items: center; gap: 7px;
  height: var(--sz-markerPill);
  padding: 0 12px 0 5px;
  border-radius: var(--r-pill);
  background: var(--bg-card);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-marker);
  font-family: var(--font-body); font-size: 12.5px; font-weight: 600;
  white-space: nowrap;
  max-width: 190px;
  z-index: 10;
}
.marker__name { overflow: hidden; text-overflow: ellipsis; }
.marker--compact { padding: 4px; gap: 0; }
.marker--compact .marker__name { display: none; }
.marker[aria-current="true"], .marker:hover {
  border-color: var(--accent);
  z-index: 12;
}
.marker__count {
  min-width: 20px; height: 20px; padding: 0 5px;
  display: grid; place-items: center;
  border-radius: var(--r-pill);
  background: var(--bg-sheet);
  font-family: var(--font-display); font-size: 11.5px; font-weight: 700;
}

/* 내 위치 */
.me {
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--accent);
  border: 3px solid var(--bg-map);
  z-index: 11;
  display: grid; place-items: center;
}
.me::before {
  content: ""; position: absolute;
  width: 88px; height: 88px; border-radius: 50%;
  background: var(--accent-halo);
  z-index: -1;
}
.me__heading {
  width: 0; height: 0;
  border-left: 5px solid transparent; border-right: 5px solid transparent;
  border-bottom: 8px solid var(--accent-ink);
  transform: translateY(-1px);
}

/* 지도 컨트롤 스택 */
.map-controls {
  position: absolute; right: var(--sp-gutter);
  bottom: calc(var(--sheet-peek, 116px) + 16px);
  display: flex; flex-direction: column; gap: 10px;
  z-index: 15;
  transition: bottom var(--motion-sheet);
}

/* 좌하단 상태 알약 */
.status-pill {
  position: absolute; left: var(--sp-gutter);
  bottom: calc(var(--sheet-peek, 116px) + 16px);
  display: inline-flex; align-items: center; gap: 7px;
  height: 28px; padding: 0 11px;
  border-radius: var(--r-pill);
  background: var(--bg-card); border: 1px solid var(--line);
  font-size: 11.5px; color: var(--text-dim);
  z-index: 15; max-width: calc(100% - 140px);
  transition: bottom var(--motion-sheet);
}
.status-pill__dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); flex: none; }
.status-pill[data-state="offline"] .status-pill__dot { background: var(--text-faint); }
.status-pill[data-state="syncing"] .status-pill__dot { background: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .3; } }
.status-pill__text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.app[data-detent="mid"] .map-controls,   .app[data-detent="mid"] .status-pill   { bottom: calc(100dvh - 252px + 16px); }
.app[data-detent="detail"] .map-controls,.app[data-detent="detail"] .status-pill{ bottom: calc(100dvh - 196px + 16px); }
.app[data-detent="full"] .map-controls,  .app[data-detent="full"] .status-pill  { opacity: 0; pointer-events: none; }

/* ---------------------------------------------------------------- sheet */
.sheet {
  position: fixed; left: 0; right: 0; top: 56px;
  height: calc(100dvh - 56px);
  z-index: 40;
  display: flex; flex-direction: column;
  background: var(--bg-sheet);
  border-top: 1px solid var(--line);
  border-radius: var(--r-sheet) var(--r-sheet) 0 0;
  box-shadow: var(--shadow-sheet);
  transform: translateY(calc(var(--sheet-top) - 56px));
  transition: transform var(--motion-sheet);
  touch-action: none;
}
.app[data-detent="peek"]   { --sheet-top: calc(100dvh - var(--sheet-peek, 116px)); }
.app[data-detent="mid"]    { --sheet-top: 252px; }
.app[data-detent="detail"] { --sheet-top: 196px; }
.app[data-detent="full"]   { --sheet-top: 56px; }
.app.is-dragging .sheet { transition: none; }

.sheet__handle {
  flex: none;
  padding: 10px 0 6px;
  display: grid; place-items: center;
  cursor: grab;
}
.sheet__handle::after {
  content: ""; width: var(--sz-handleW); height: var(--sz-handleH);
  border-radius: var(--r-pill); background: var(--handle);
}

.sheet__body {
  flex: 1 1 auto; min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(28px + env(safe-area-inset-bottom));
}
.app[data-detent="peek"] .sheet__body { overflow: hidden; }

/* peek 요약 행 — 전체가 목록을 여는 버튼이다 */
.peek {
  display: flex; align-items: center; gap: var(--sp-s3);
  width: 100%;
  padding: 2px var(--sp-gutter) 0;
  text-align: left;
  min-height: 56px;
}
.peek__text { flex: 1 1 auto; min-width: 0; }
.peek__title { font-size: 15.5px; font-weight: 600; }
.peek__sub { font-size: 12.5px; color: var(--text-dim); }
.peek__dist { display: flex; align-items: baseline; gap: 3px; }
.peek__dist b { font-family: var(--font-display); font-size: 24px; font-weight: 700; color: var(--accent); }
.peek__dist span { font-size: 12.5px; color: var(--text-dim); }

/* 목록 헤더 */
.sheet__head {
  display: flex; align-items: center; gap: var(--sp-s3);
  padding: 4px var(--sp-gutter) 12px;
}
.sheet__title { font-family: var(--font-display); font-size: 19px; font-weight: 700; margin: 0; margin-right: auto; }

/* ---------------------------------------------------------------- POI 행 */
.poi {
  display: flex; align-items: center; gap: 13px;
  padding: 14px var(--sp-gutter);
  border-top: 1px solid var(--line-soft);
  width: 100%;
  text-align: left;
}
.poi:first-of-type { border-top: 0; }
.poi:hover, .poi[aria-current="true"] { background: var(--bg-card); }
.poi__text { flex: 1 1 auto; min-width: 0; }
.poi__name {
  font-size: 15.5px; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.poi__sub {
  font-size: 12.5px; color: var(--text-dim);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.poi__dist { display: flex; align-items: baseline; gap: 3px; flex: none; }
.poi__dist b { font-family: var(--font-display); font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.poi__dist span { font-size: 12.5px; color: var(--text-dim); }
.poi > .ic:last-child { color: var(--text-faint); }

/* 빈 상태 / 스켈레톤 */
.empty {
  padding: 26px var(--sp-gutter) 8px;
  text-align: center;
}
.empty p { margin: 0 0 14px; font-size: 13.5px; color: var(--text-dim); }

.skel { display: flex; align-items: center; gap: 13px; padding: 14px var(--sp-gutter); border-top: 1px solid var(--line-soft); }
.skel__b { background: var(--bg-card); border-radius: 8px; animation: shimmer 1.4s ease-in-out infinite; }
.skel__badge { width: var(--sz-badgeList); height: var(--sz-badgeList); border-radius: var(--r-badge); }
.skel__line { height: 12px; }
@keyframes shimmer { 50% { opacity: .45; } }

.sheet__foot {
  padding: 18px var(--sp-gutter) 0;
  font-size: 11.5px; color: var(--text-faint);
  text-align: center;
}

/* ---------------------------------------------------------------- 상세 */
.detail { display: flex; flex-direction: column; min-height: 100%; padding: 0 var(--sp-gutter) 30px; }
.detail__head { display: flex; align-items: flex-start; gap: 13px; padding: 6px 0 14px; }
.detail__title {
  font-family: var(--font-display); font-size: 21px; font-weight: 700;
  margin: 0 0 3px; text-wrap: pretty; line-height: 1.22;
}
.detail__sub { font-size: 12.5px; color: var(--text-dim); }

.meta-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; }
.meta-chip {
  display: inline-flex; align-items: center; gap: 5px;
  height: 28px; padding: 0 10px;
  border-radius: var(--r-metaChip);
  background: var(--bg-card); border: 1px solid var(--line);
  font-size: 12.5px; color: var(--text-dim);
}
.meta-chip--code { font-family: var(--font-display); font-weight: 600; letter-spacing: .04em; color: var(--text); }
.meta-chip--ok { background: var(--ok-bg); border-color: transparent; color: var(--ok); font-weight: 600; }

.spec {
  background: var(--bg-card); border: 1px solid var(--line);
  border-radius: var(--r-card);
  margin-bottom: 14px;
}
.spec__row {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 14px;
  border-top: 1px solid var(--line-soft);
}
.spec__row:first-child { border-top: 0; }
.spec__label { font-size: 13px; color: var(--text-dim); }
.spec__value { font-family: var(--font-display); font-size: 15px; font-weight: 600; text-align: right; }

.warn {
  display: flex; gap: 11px;
  padding: 13px 14px;
  border-radius: var(--r-card);
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  margin-bottom: 14px;
}
.warn .ic { color: var(--warn); margin-top: 1px; }
.warn p { margin: 0; font-size: 13.5px; line-height: 1.45; }

.detail__actions { margin-top: auto; padding-top: 10px; display: grid; gap: 10px; }
.detail__updated { margin-top: 14px; text-align: center; font-size: 11.5px; color: var(--text-faint); }

/* ---------------------------------------------------------------- 토스트 */
.toast-host {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--sheet-peek, 116px) + 72px);
  z-index: 60;
  display: flex; flex-direction: column; gap: 8px;
  width: min(420px, calc(100vw - 32px));
  pointer-events: none;
}
.toast {
  display: flex; align-items: center; gap: 9px;
  padding: 12px 14px;
  border-radius: var(--r-card);
  background: var(--bg-card); border: 1px solid var(--line);
  box-shadow: var(--shadow-toast);
  font-size: 13.5px;
  animation: toast-in var(--motion-sheet);
}
.toast .ic { color: var(--accent); }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } }

/* ---------------------------------------------------------------- 설정 */
.dialog {
  position: fixed; inset: 0; z-index: 70;
  border: 0; padding: 0; margin: 0;
  width: 100%; max-width: none; height: 100%; max-height: none;
  background: transparent;
}
.dialog::backdrop { background: var(--scrim); backdrop-filter: blur(2px); }
.dialog__panel {
  position: absolute; left: 0; right: 0; bottom: 0;
  max-height: 86dvh; overflow-y: auto;
  background: var(--bg-sheet);
  border-top: 1px solid var(--line);
  border-radius: var(--r-sheet) var(--r-sheet) 0 0;
  padding: 10px var(--sp-gutter) calc(28px + env(safe-area-inset-bottom));
}
.set-group { margin-bottom: 20px; }
.set-group > h3 {
  font-family: var(--font-display); font-size: 13px; font-weight: 600;
  color: var(--text-dim); text-transform: uppercase; letter-spacing: .05em;
  margin: 0 0 9px;
}
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button {
  flex: 1 1 auto; min-width: 72px;
  height: var(--sz-chip); padding: 0 12px;
  border-radius: var(--r-chip);
  background: var(--bg-card); border: 1px solid var(--line);
  font-family: var(--font-display); font-size: 13px; font-weight: 600;
}
.seg button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.set-rows { background: var(--bg-card); border: 1px solid var(--line); border-radius: var(--r-card); }

/* ---------------------------------------------------------------- 데스크톱 */
@media (min-width: 768px) {
  /* 시트를 좌측 고정 사이드바로 바꾸고 지도를 전체 높이로.
     헤더(행1) + 사이드바(행2)가 1열, 지도가 2열 전체를 차지한다. */
  .app {
    display: grid;
    grid-template-columns: var(--sz-sidebarMd) 1fr;
    grid-template-rows: auto 1fr;
  }
  .header {
    grid-column: 1; grid-row: 1;
    padding-top: 20px;
    background: var(--bg-sheet);
    z-index: 45;
  }
  .map-wrap { grid-column: 2; grid-row: 1 / span 2; }

  .sheet {
    position: relative;
    grid-column: 1; grid-row: 2;
    top: 0; height: 100%; min-height: 0;
    transform: none !important;
    border-radius: 0;
    border-top: 0;
    border-right: 1px solid var(--line);
    box-shadow: none;
    touch-action: auto;
  }
  .sheet__handle { display: none; }
  .sheet__body { padding-bottom: 20px; }
  .map-fade { display: none; }
  .map-controls, .status-pill { bottom: var(--sp-gutter) !important; opacity: 1 !important; pointer-events: auto !important; }
  .toast-host { left: auto; right: var(--sp-gutter); transform: none; bottom: 84px; }

  /* peek 요약 행은 모바일 전용 — 데스크톱은 목록이 항상 펼쳐져 있다 */
  .peek { display: none; }

  .dialog__panel {
    inset: auto auto auto 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: min(520px, calc(100vw - 48px));
    border-radius: var(--r-panel);
    border: 1px solid var(--line);
  }
}

@media (min-width: 1024px) {
  .app { grid-template-columns: var(--sz-sidebarLg) 1fr; }
}

/* ---------------------------------------------------------------- 모션 */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
