:root {
  --bg: #f5f6f8;
  --surface: rgba(255, 255, 255, 0.92);
  --surface-solid: #ffffff;
  --surface-2: #f0f2f5;
  --text: #11151c;
  --muted: #5d6573;
  --border: rgba(17, 21, 28, 0.08);
  --accent: #2563eb;
  --accent-soft: rgba(37, 99, 235, 0.12);
  --ok: #16a34a;
  --warn: #d97706;
  --danger: #dc2626;
  --shadow: 0 8px 28px rgba(15, 23, 42, 0.14), 0 1px 3px rgba(15, 23, 42, 0.08);
  --radius: 16px;
  --plane-stroke: rgba(255, 255, 255, 0.95);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: rgba(24, 28, 35, 0.92);
    --surface-solid: #181c23;
    --surface-2: #222731;
    --text: #e8ebf0;
    --muted: #9aa3b2;
    --border: rgba(255, 255, 255, 0.08);
    --accent: #60a5fa;
    --accent-soft: rgba(96, 165, 250, 0.16);
    --danger: #f87171;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.45), 0 1px 3px rgba(0, 0, 0, 0.3);
    --plane-stroke: rgba(14, 17, 22, 0.9);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

svg { display: block; }

#map {
  position: fixed;
  inset: 0;
  background: var(--bg);
}

.leaflet-container { font: inherit; background: var(--bg); }
.leaflet-control-attribution {
  background: var(--surface) !important;
  color: var(--muted) !important;
  font-size: 10px;
  border-radius: 8px 0 0 0;
}
.leaflet-control-attribution a { color: var(--muted) !important; }
.leaflet-control-zoom { display: none; }

/* ---------- Top bar ---------- */
.topbar {
  position: fixed;
  top: calc(12px + var(--safe-top));
  left: 12px;
  right: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  pointer-events: none;
  z-index: 1000;
}
.topbar > * { pointer-events: auto; }

.brand, .status {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  border-radius: 999px;
  height: 40px;
  padding: 0 14px;
}
.brand { font-weight: 700; letter-spacing: -0.01em; }
.brand svg { width: 20px; height: 20px; fill: var(--accent); transform: rotate(45deg); }

.status { font-size: 13px; color: var(--muted); min-width: 0; }
.status #status-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--muted);
  flex: none;
}
.dot.live { background: var(--ok); box-shadow: 0 0 0 0 rgba(22, 163, 74, 0.5); animation: pulse 2s infinite; }
.dot.error { background: var(--danger); }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(22, 163, 74, 0.5); }
  70% { box-shadow: 0 0 0 8px rgba(22, 163, 74, 0); }
  100% { box-shadow: 0 0 0 0 rgba(22, 163, 74, 0); }
}

/* ---------- Floating buttons ---------- */
.controls {
  position: fixed;
  right: 12px;
  bottom: calc(28px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 1000;
}
.fab {
  width: 48px; height: 48px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow);
  display: grid; place-items: center;
  cursor: pointer;
  transition: transform 0.15s ease;
}
.fab:active { transform: scale(0.94); }
.fab svg { width: 22px; height: 22px; fill: none; stroke: var(--text); stroke-width: 2; stroke-linecap: round; }
.fab.active svg { stroke: var(--accent); }
.fab:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Zoom buttons: only for mouse/trackpad users; touch devices pinch to zoom. */
.zoom-group { display: none; flex-direction: column; gap: 10px; margin-bottom: 6px; }
@media (hover: hover) and (pointer: fine) {
  .zoom-group { display: flex; }
}

/* ---------- Legend ---------- */
.legend {
  position: fixed;
  left: 12px;
  bottom: calc(28px + var(--safe-bottom));
  display: flex; align-items: center; gap: 6px;
  font-size: 11px; color: var(--muted);
  background: var(--surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 10px;
  box-shadow: var(--shadow);
  z-index: 900;
}
.legend-bar {
  width: 90px; height: 6px; border-radius: 3px;
  background: linear-gradient(90deg,
    hsl(28 92% 52%), hsl(55 85% 48%), hsl(120 60% 42%),
    hsl(180 65% 42%), hsl(220 75% 55%), hsl(275 65% 58%));
}

/* ---------- Aircraft markers ---------- */
.plane-icon { background: none; border: none; }
.plane-icon .plane {
  width: 100%; height: 100%;
  transition: transform 0.4s linear;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.35));
  cursor: pointer;
}
.plane-icon .plane path {
  stroke: var(--plane-stroke);
  stroke-width: 1;
  paint-order: stroke;
}
.plane-icon.selected .plane { filter: drop-shadow(0 0 6px var(--accent)) drop-shadow(0 1px 2px rgba(0,0,0,0.4)); }
.plane-icon.selected .plane path { stroke: var(--accent); stroke-width: 1.6; }
.plane-label {
  position: absolute;
  left: 50%; top: 100%;
  transform: translateX(-50%);
  margin-top: 2px;
  font-size: 10px; font-weight: 600;
  white-space: nowrap;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
  pointer-events: none;
  display: none;
}
.show-labels .plane-label, .plane-icon.selected .plane-label { display: block; }

.me-dot {
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent);
  border: 3px solid #fff;
  box-shadow: 0 0 0 6px var(--accent-soft), 0 2px 6px rgba(0,0,0,0.3);
}

/* ---------- Sheets (bottom on phones, side panel on larger screens) ---------- */
.sheet {
  position: fixed;
  z-index: 1100;
  left: 0; right: 0; bottom: 0;
  max-height: 72vh;
  display: flex; flex-direction: column;
  background: var(--surface-solid);
  border-top-left-radius: 22px;
  border-top-right-radius: 22px;
  box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.18);
  padding-bottom: var(--safe-bottom);
  animation: sheet-up 0.25s ease;
}
.sheet[hidden] { display: none; }
@keyframes sheet-up { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }

.sheet-grip {
  width: 38px; height: 5px; border-radius: 3px;
  background: var(--border);
  background: color-mix(in srgb, var(--muted) 40%, transparent);
  margin: 8px auto 0;
  flex: none;
}
.sheet-head {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 12px;
  padding: 10px 18px 12px;
  flex: none;
}
.sheet-head h2 { margin: 0; font-size: 22px; letter-spacing: -0.02em; }
.sheet-head .sub { color: var(--muted); font-size: 14px; margin-top: 2px; }
.sheet-body { overflow-y: auto; padding: 0 14px 18px; -webkit-overflow-scrolling: touch; }

.icon-btn {
  width: 34px; height: 34px; border-radius: 50%;
  border: none; background: var(--surface-2);
  display: grid; place-items: center; cursor: pointer; flex: none;
}
.icon-btn svg { width: 16px; height: 16px; stroke: var(--muted); stroke-width: 2.4; stroke-linecap: round; fill: none; }

@media (min-width: 760px) {
  .sheet {
    left: 12px; right: auto; bottom: auto;
    top: calc(64px + var(--safe-top));
    width: 380px;
    max-height: calc(100vh - 140px - var(--safe-top));
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    animation: sheet-in 0.2s ease;
  }
  .sheet-grip { display: none; }
  .sheet-head { padding-top: 16px; }
  @keyframes sheet-in { from { transform: translateX(-12px); opacity: 0; } to { transform: none; opacity: 1; } }
}

/* ---------- Detail content ---------- */
.photo {
  margin: 0 0 12px;
  border-radius: 14px;
  overflow: hidden;
  background: var(--surface-2);
  position: relative;
  aspect-ratio: 16 / 9;
}
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo figcaption {
  position: absolute; left: 0; right: 0; bottom: 0;
  font-size: 11px; color: #fff;
  padding: 16px 10px 6px;
  background: linear-gradient(transparent, rgba(0,0,0,0.6));
}
.photo figcaption a { color: #fff; }

.card {
  background: var(--surface-2);
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 12px;
}
.card h3 {
  margin: 0 0 10px;
  font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--muted); font-weight: 600;
}

.route-ends { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.route .end { min-width: 0; flex: 1; }
.route .end.right { text-align: right; }
.route .code { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; }
.route .city { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.route-plane { width: 22px; height: 22px; fill: var(--accent); transform: rotate(90deg); flex: none; }
.progress { height: 6px; border-radius: 3px; background: var(--border); background: color-mix(in srgb, var(--muted) 22%, transparent); margin-top: 12px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--accent); border-radius: 3px; transition: width 0.6s ease; }
.route-meta { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-top: 6px; }

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px 14px;
  margin: 0;
}
.grid div { min-width: 0; }
.grid dt { font-size: 12px; color: var(--muted); margin-bottom: 2px; }
.grid dd { margin: 0; font-size: 15px; font-weight: 600; overflow-wrap: anywhere; }
.grid dd small { font-weight: 500; color: var(--muted); }

.links { display: flex; gap: 8px; flex-wrap: wrap; }
.links a {
  flex: 1 1 auto;
  text-align: center;
  text-decoration: none;
  color: var(--accent);
  background: var(--accent-soft);
  font-size: 14px; font-weight: 600;
  padding: 10px 12px;
  border-radius: 12px;
}

.badge {
  display: inline-block;
  font-size: 11px; font-weight: 700;
  padding: 2px 6px; border-radius: 6px;
  vertical-align: middle;
  margin-left: 6px;
  background: var(--accent-soft); color: var(--accent);
}
.badge.danger { background: rgba(220, 38, 38, 0.14); color: var(--danger); }

/* ---------- List filters ---------- */
.filters { padding: 0 18px 8px; flex: none; }
.seg {
  display: flex;
  background: var(--surface-2);
  border-radius: 10px;
  padding: 3px;
  margin-bottom: 10px;
}
.seg button {
  flex: 1;
  border: none; background: none;
  padding: 7px 4px;
  border-radius: 8px;
  font-size: 13px; font-weight: 600; color: var(--muted);
  cursor: pointer;
}
.seg button[aria-pressed="true"] { background: var(--surface-solid); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,0.12); }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.chips::-webkit-scrollbar { display: none; }
.chips button {
  flex: none;
  border: 1px solid var(--border);
  background: none;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px; font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}
.chips button span { color: var(--muted); font-weight: 500; margin-left: 2px; }
.chips button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.chips button[aria-pressed="true"] span { color: rgba(255,255,255,0.8); }

.route-note { margin: 0; font-size: 13px; color: var(--muted); line-height: 1.45; }

/* ---------- Nearby list ---------- */
.nearby { list-style: none; margin: 0; padding: 0 10px 14px; overflow-y: auto; }
.nearby li button {
  width: 100%;
  display: grid;
  grid-template-columns: 28px 1fr auto;
  align-items: center;
  gap: 12px;
  text-align: left;
  background: none; border: none;
  padding: 10px 8px;
  border-radius: 12px;
  cursor: pointer;
}
.nearby li button:hover, .nearby li button:focus-visible { background: var(--surface-2); outline: none; }
.nearby .mini { width: 26px; height: 26px; }
.nearby .mini path { stroke: var(--plane-stroke); stroke-width: 1; }
.nearby .name { font-weight: 600; font-size: 15px; }
.nearby .meta { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nearby .right { text-align: right; font-size: 13px; color: var(--muted); }
.nearby .right strong { display: block; color: var(--text); font-size: 15px; }
.nearby .empty { color: var(--muted); text-align: center; padding: 24px 12px; }

/* ---------- Location sheet ---------- */
.place-title { display: flex; align-items: center; gap: 12px; min-width: 0; }
.place-icon {
  width: 42px; height: 42px; border-radius: 12px; flex: none;
  display: grid; place-items: center;
  background: var(--accent-soft);
}
.place-icon svg { width: 22px; height: 22px; fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.place-title .sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.place { display: flex; flex-direction: column; gap: 14px; padding: 4px 18px 20px; }

.callout {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 12px 14px;
  border-radius: 12px;
  font-size: 14px; line-height: 1.45;
}
.callout p { margin: 0; }
.callout svg { width: 20px; height: 20px; flex: none; margin-top: 1px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.callout.danger {
  color: var(--danger);
  background: rgba(220, 38, 38, 0.08);
  border: 1px solid rgba(220, 38, 38, 0.25);
}

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-weight: 600;
  cursor: pointer;
  flex: none;
  transition: filter 0.15s ease, transform 0.1s ease;
}
.btn.primary { background: var(--accent); border-color: transparent; color: var(--surface-solid); }
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: scale(0.98); }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.btn:focus-visible, .link-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }

.place label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin: 0 2px 8px; }
.search-field {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 4px 4px 12px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.search-field:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search-field > svg { width: 18px; height: 18px; flex: none; fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; }
.search-field input {
  flex: 1; min-width: 0;
  min-height: 40px; padding: 0 4px;
  border: none; outline: none;
  background: transparent;
  color: var(--text);
  font: inherit; font-size: 16px; /* 16px stops iOS zooming in on focus */
}
.search-field input::-webkit-search-cancel-button { cursor: pointer; }
.search-field .btn { min-height: 40px; border-radius: 10px; }
.place .hint { margin: 8px 2px 0; font-size: 12px; color: var(--muted); }
.place .hint code { font-size: 12px; padding: 1px 5px; border-radius: 6px; background: var(--surface-2); }

.place-results { list-style: none; margin: -4px -8px 0; padding: 0; }
.place-results li + li { border-top: 1px solid var(--border); }
.place-results li button {
  width: 100%; display: flex; align-items: center; gap: 12px;
  text-align: left; background: none; border: none;
  padding: 11px 8px; border-radius: 12px; cursor: pointer;
}
.place-results li button > svg { width: 20px; height: 20px; flex: none; fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.place-results li button:hover, .place-results li button:focus-visible { background: var(--surface-2); outline: none; }
.place-results .name { display: block; font-weight: 600; font-size: 15px; }
.place-results .meta { display: block; font-size: 13px; color: var(--muted); margin-top: 1px; }
.place-results .empty { color: var(--muted); text-align: center; padding: 16px 12px; }
.link-btn {
  align-self: center;
  background: none; border: none; padding: 4px 8px;
  color: var(--muted); font-size: 13px; text-decoration: underline; cursor: pointer;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  top: calc(64px + var(--safe-top));
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  padding: 10px 16px;
  border-radius: 12px;
  box-shadow: var(--shadow);
  z-index: 1200;
}
.toast[hidden] { display: none; }

@media (max-width: 759px) {
  /* Keep the floating controls clear of an open bottom sheet. */
  body.sheet-open .controls, body.sheet-open .legend { display: none; }
}
