/* ===== 기본 리셋 & 변수 ===== */
:root {
  --bg: #0b1020;
  --bg-card: #141b2d;
  --bg-elevated: #1a2338;
  --border: #243049;
  --text: #e8eefc;
  --text-muted: #8b9bb8;
  --accent: #3b82f6;
  --accent-soft: rgba(59, 130, 246, 0.15);
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --radius: 14px;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans KR", sans-serif;
}

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

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(16px + var(--safe-bottom));
}

/* ===== 헤더 ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  background: rgba(11, 16, 32, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  font-size: 28px;
  line-height: 1;
}

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

.status-text {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.menu-btn {
  width: 40px;
  height: 40px;
  border: none;
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: 10px;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.menu-btn:active {
  background: var(--border);
}

/* ===== 메뉴 오버레이 ===== */
.menu-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: flex-end;
  transition: opacity 0.25s;
}

.menu-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.menu-panel {
  width: min(300px, 85vw);
  height: 100%;
  background: var(--bg-card);
  padding: calc(20px + var(--safe-top)) 20px calc(20px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  box-shadow: -8px 0 30px rgba(0, 0, 0, 0.4);
  transform: translateX(0);
  transition: transform 0.25s ease;
}

.menu-overlay.hidden .menu-panel {
  transform: translateX(100%);
}

.menu-panel h2 {
  font-size: 15px;
  color: var(--text-muted);
  margin-bottom: 16px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.menu-panel ul {
  list-style: none;
  flex: 1;
}

.menu-panel li button {
  width: 100%;
  text-align: left;
  padding: 14px 12px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
}

.menu-panel li button:hover,
.menu-panel li button:active {
  background: var(--accent-soft);
}

.menu-close {
  margin-top: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  border-radius: 10px;
  font-size: 14px;
  cursor: pointer;
}

/* ===== 뷰 ===== */
.view {
  display: none;
  flex: 1;
  padding: 12px 14px 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.view.active {
  display: block;
}

.view-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}

.view-head h2 {
  font-size: 18px;
}

.back-btn {
  border: none;
  background: var(--bg-elevated);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 14px;
  cursor: pointer;
}

/* ===== 카드 ===== */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
  gap: 8px;
}

.card-head h2,
.card h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.card h3 {
  margin-bottom: 12px;
}

/* ===== 그리드 & 필드 ===== */
.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.field {
  background: var(--bg-elevated);
  border-radius: 10px;
  padding: 10px 12px;
}

.field-label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.field-value {
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  word-break: break-all;
}

/* ===== 뱃지 ===== */
.badge {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 20px;
  background: var(--bg-elevated);
  color: var(--text-muted);
  white-space: nowrap;
}

.badge.fresh {
  background: rgba(34, 197, 94, 0.15);
  color: var(--success);
}

.badge.stale {
  background: rgba(245, 158, 11, 0.15);
  color: var(--warning);
}

.badge.error {
  background: rgba(239, 68, 68, 0.15);
  color: var(--danger);
}

/* ===== 버튼 ===== */
.mini-btn {
  border: none;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.mini-btn:active {
  opacity: 0.8;
}

.mini-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.map-controls,
.log-actions {
  display: flex;
  gap: 6px;
}

/* ===== 맵 ===== */
.map {
  height: 240px;
  border-radius: 12px;
  overflow: hidden;
  background: var(--bg-elevated);
  z-index: 1;
}

.map-hint {
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
  margin-top: 8px;
}

/* Leaflet overrides */
.leaflet-container {
  background: var(--bg-elevated) !important;
  font-family: var(--font) !important;
}

.leaflet-control-zoom a {
  background: var(--bg-card) !important;
  color: var(--text) !important;
  border-color: var(--border) !important;
}

.leaflet-control-attribution {
  font-size: 9px !important;
  background: rgba(11, 16, 32, 0.7) !important;
  color: var(--text-muted) !important;
}

/* ===== 주소 ===== */
.address-box {
  background: var(--bg-elevated);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 13px;
  line-height: 1.55;
  color: var(--text);
  min-height: 48px;
}

/* ===== 기기/네트워크 정보 ===== */
.device-info {
  font-size: 13px;
  line-height: 1.7;
  color: var(--text);
}

.device-info .row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 0;
  border-bottom: 1px solid rgba(36, 48, 73, 0.5);
}

.device-info .row:last-child {
  border-bottom: none;
}

.device-info .label {
  color: var(--text-muted);
  flex-shrink: 0;
}

.device-info .value {
  text-align: right;
  font-weight: 500;
  word-break: break-all;
}

/* ===== 로그 ===== */
.log-box {
  background: #0a0e1a;
  border-radius: 10px;
  padding: 10px 12px;
  font-family: "SF Mono", "Fira Code", "Consolas", monospace;
  font-size: 11px;
  line-height: 1.55;
  max-height: 180px;
  overflow-y: auto;
  color: #a0b4d0;
}

.log-box .log-entry {
  margin-bottom: 3px;
}

.log-box .log-time {
  color: #5a6e8a;
}

.log-box .log-info { color: #60a5fa; }
.log-box .log-ok { color: #4ade80; }
.log-box .log-warn { color: #fbbf24; }
.log-box .log-err { color: #f87171; }

/* ===== 푸터 ===== */
.foot {
  display: flex;
  justify-content: space-between;
  padding: 10px 4px 4px;
  font-size: 11px;
  color: var(--text-muted);
}

/* ===== 차트 ===== */
.chart {
  height: 160px;
  background: var(--bg-elevated);
  border-radius: 10px;
  position: relative;
  overflow: hidden;
}

.chart canvas {
  width: 100%;
  height: 100%;
  display: block;
}

/* ===== 토스트 ===== */
.toast {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 20px;
  border-radius: 12px;
  font-size: 13px;
  z-index: 300;
  box-shadow: var(--shadow);
  transition: opacity 0.3s, transform 0.3s;
  max-width: 90vw;
  text-align: center;
}

.toast.hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(12px);
  pointer-events: none;
}

/* ===== 로딩 ===== */
.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 400;
  background: rgba(11, 16, 32, 0.88);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  transition: opacity 0.3s;
}

.loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-overlay p {
  font-size: 14px;
  color: var(--text-muted);
}

/* ===== 추가 기능 UI ===== */
.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.action-row .mini-btn {
  flex: 1;
  min-width: 100px;
  text-align: center;
  padding: 10px 8px;
}

.compass-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
  margin-top: 8px;
}

.compass {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--bg-elevated);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.compass-needle {
  width: 4px;
  height: 32px;
  background: linear-gradient(to bottom, var(--danger) 50%, var(--text) 50%);
  border-radius: 2px;
  transform-origin: center center;
  transition: transform 0.3s ease;
}

.compass-label {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
}

.track-stats {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.stat-box {
  background: var(--bg-elevated);
  border-radius: 10px;
  padding: 10px 8px;
  text-align: center;
}

.stat-box .stat-val {
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
}

.stat-box .stat-lbl {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
}

.toggle {
  position: relative;
  width: 44px;
  height: 24px;
}

.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle .slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 24px;
  cursor: pointer;
  transition: 0.2s;
}

.toggle .slider::before {
  content: "";
  position: absolute;
  width: 18px;
  height: 18px;
  left: 3px;
  top: 3px;
  background: white;
  border-radius: 50%;
  transition: 0.2s;
}

.toggle input:checked + .slider {
  background: var(--accent);
}

.toggle input:checked + .slider::before {
  transform: translateX(20px);
}

/* ===== 반응형 ===== */
@media (min-width: 480px) {
  .map {
    height: 300px;
  }
  .grid-2 {
    grid-template-columns: 1fr 1fr 1fr;
  }
}
